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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM