• CentOS 不间断会话(ssh关闭后如何保证程序继续运行)(nohup和screen)


    当使用ssh与远程主机的会话被关闭时,在远程主机上运行的命令也随之被中断。

    就是ssh 打开以后,bash等都是他的子程序,一旦ssh关闭,系统将所有相关进程杀掉!! 导致一旦ssh关闭,执行中的任务就取消了。

    守护进程不受此影响, 因为守护进程比较特殊, 不属于sshd这个进程组 而是单独的进程组,所以就算关闭了ssh,和他也没有任何关系。

    解决办法:

    1、使用nohup命令来运行程序

    [es@localhost ~]$ nohup /opt/elasticsearch-6.5.1/bin/elasticsearch
    nohup: 忽略输入并把输出追加到"nohup.out"
    
    或者
    [es@localhost ~]$ nohup /opt/elasticsearch-6.5.1/bin/elasticsearch &
    [1] 3370
    [es@localhost ~]$ nohup: 忽略输入并把输出追加到"nohup.out"
    #使用tail -f来查看输出信息
    [es@localhost ~]$ tail -f nohup.out
    [2019-01-21T11:45:19,625][INFO ][o.w.a.d.Monitor          ] 
    。。。 。。。

    nohup命令比较简单,只能做到把程序放入后台运行,并且ssh关闭后不中断,无法实施查看运行情况(除了看nohup.out),无法进行人机交互操作。

    如果要实现这些,中断ssh后,重新连上还能继续操作,则需要screen这个软件。

    2、screen 是一款能够实现多窗口远程控制的开源服务程序,简单来说就是为了解决网络异常中断或为了同时控制多个远程终端窗口而设计的程序。

    用户还可以使用 screen 服务程序同时在多个远程会话中自由切换和共享会话。

    2.1、安装

    检查一下REPO里cdrom的配置

    [root@localhost yum.repos.d]# cat CentOS-Media.repo
    # CentOS-Media.repo
    #
    #  This repo can be used with mounted DVD media, verify the mount point for
    #  CentOS-7.  You can use this repo and yum to install items directly off the
    #  DVD ISO that we release.
    #
    # To use this repo, put in your DVD and use it with the other repos too:
    #  yum --enablerepo=c7-media [command]
    #  
    # or for ONLY the media repo, do this:
    #
    #  yum --disablerepo=* --enablerepo=c7-media [command]
    
    [c7-media]
    name=CentOS-$releasever - Media
    baseurl=file:///media/CentOS/
            file:///media/cdrom/
            file:///media/cdrecorder/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    挂载centos7镜像来安装

    [root@localhost yum.repos.d]# mkdir -p /media/cdrom

    [root@localhost yum.repos.d]# mount /dev/cdrom /media/cdrom mount: /dev/sr0 写保护,将以只读方式挂载 [root@localhost yum.repos.d]# yum install screen 已安装: screen.x86_64 0:4.1.0-0.25.20120314git3c2946.el7 完毕! [root@localhost yum.repos.d]# screen --help

    2.2、恢复session

    [root@localhost ~]# screen -ls
    No Sockets found in /var/run/screen/S-root.
    
    #创建一个session,名字es,屏幕一闪,实际已经进入screen
    [root@localhost ~]# screen -S es
    
    [root@localhost ~]# screen -ls
    There is a screen on:
            3846.es (Attached)
    1 Socket in /var/run/screen/S-root.
    
    #执行ping,然后直接关闭ssh窗口 [root@localhost
    ~]# ping 192.168.1.103 PING 192.168.1.103 (192.168.1.103) 56(84) bytes of data. 64 bytes from 192.168.1.103: icmp_seq=1 ttl=128 time=0.648 ms
    #重新登录ssh,使用screen -ls查看,有session是detached [root@localhost
    ~]# screen -ls There is a screen on: 3846.es (Detached) 1 Socket in /var/run/screen/S-root.
    #恢复窗口,ping一直在运行 [root@localhost
    ~]# screen -r 3846.es ... ... 64 bytes from 192.168.1.103: icmp_seq=83 ttl=128 time=0.904 ms 64 bytes from 192.168.1.103: icmp_seq=84 ttl=128 time=1.07 ms 64 bytes from 192.168.1.103: icmp_seq=85 ttl=128 time=0.773 ms 64 bytes from 192.168.1.103: icmp_seq=86 ttl=128 time=1.26 ms ^C --- 192.168.1.103 ping statistics --- 86 packets transmitted, 86 received, 0% packet loss, time 85134ms rtt min/avg/max/mdev = 0.467/1.051/4.810/0.535 ms 打exit退出screen [screen is terminating]

    也可以不手动创建session,直接使用screen 命令执行要运行的命令,这样在命令中的一切操作也都会被记录下来,当命令执行结束后 screen 会话也会自动结束。

    这种情况下,session名字是系统自动创建的。

    [es@localhost ~]$ screen /opt/elasticsearch-6.5.1/bin/elasticsearch
    
    断开ssh后,重新查看
    [es@localhost ~]$ screen -ls
    There is a screen on:
        4104.pts-0.localhost    (Detached)
    1 Socket in /var/run/screen/S-es.
    
    用kill命令杀掉elasticsearch后,screen自动终止

    2.3、共享session,可以多个终端连入同一个session,所有操作在连入的终端上都可以显示,所有终端都可以操作。

    [root@localhost network-scripts]# screen -ls
    There are screens on:
        4769.ping3    (Detached)
        4704.myping    (Detached)
    2 Sockets in /var/run/screen/S-root.
    
    [root@localhost network-scripts]# screen -x ping3
  • 相关阅读:
    ajax 请求登录超时跳转登录页的示例代码
    [WPF]实现密码框的密码绑定
    Linq系列(5)——表达式树之案例应用
    idea设置内存大小
    idea右下角显示使用内存情况
    idea打开Run Dashboard
    java的byte[]与String相互转换
    java有包名的调用没有包名的类,用反射
    【转】查看电脑显卡型号及显卡性能
    idea关闭sonarLint自动扫描
  • 原文地址:https://www.cnblogs.com/asker009/p/10298114.html
Copyright © 2020-2023  润新知