當使用ssh與遠端主機的會話被關閉時,在遠端主機上運作的指令也随之被中斷。
就是ssh 打開以後,bash等都是他的子程式,一旦ssh關閉,系統将所有相關程序殺掉!! 導緻一旦ssh關閉,執行中的任務就取消了。
守護程序不受此影響, 因為守護程序比較特殊, 不屬于sshd這個程序組 而是單獨的程序組,是以就算關閉了ssh,和他也沒有任何關系。
解決辦法:
1、使用nohup指令來運作程式
[[email protected] ~]$ nohup /opt/elasticsearch-6.5.1/bin/elasticsearch
nohup: 忽略輸入并把輸出追加到"nohup.out"或者
[[email protected]~]$ nohup /opt/elasticsearch-6.5.1/bin/elasticsearch &[1] 3370[[email protected]~]$ nohup: 忽略輸入并把輸出追加到"nohup.out"
#使用tail -f來檢視輸出資訊
[[email protected]~]$ 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的配置
[[email protected] yum.repos.d]# cat CentOS-Media.repo
# CentOS-Media.repo
#
# This repo can be used with mounted DVD media, verify themount point for# CentOS-7. You can use this repo and yum to installitems directly off the
# DVD ISO that we release.
#
# To use this repo, putinyour DVD and use it with the other repos too:
#yum --enablerepo=c7-media [command]
#
# orfor ONLY the media repo, dothis:
#
#yum --disablerepo=\* --enablerepo=c7-media [command]
[c7-media]
name=CentOS-$releasever -Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///media/cdrecorder/
gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
挂載centos7鏡像來安裝
[[email protected] yum.repos.d]# mkdir -p /media/cdrom
[[email protected] yum.repos.d]# mount /dev/cdrom /media/cdrommount: /dev/sr0 寫保護,将以隻讀方式挂載
[[email protected]]# yum installscreen
已安裝:
screen.x86_640:4.1.0-0.25.20120314git3c2946.el7
完畢!
[[email protected]]# screen --help
2.2、恢複session
[[email protected] ~]# screen -lsNo Sockets foundin /var/run/screen/S-root.
#建立一個session,名字es,螢幕一閃,實際已經進入screen
[[email protected]~]# screen -S es
[[email protected]~]# screen -lsThere is a screen on:3846.es (Attached)1 Socket in /var/run/screen/S-root.
#執行ping,然後直接關閉ssh視窗
[[email protected]~]# ping 192.168.1.103PING192.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.648ms
#重新登入ssh,使用screen -ls檢視,有session是detached
[[email protected]~]# screen -lsThere is a screen on:3846.es (Detached)1 Socket in /var/run/screen/S-root.
#恢複視窗,ping一直在運作
[[email protected]~]# screen -r 3846.es
... ...64 bytes from 192.168.1.103: icmp_seq=83 ttl=128 time=0.904ms64 bytes from 192.168.1.103: icmp_seq=84 ttl=128 time=1.07ms64 bytes from 192.168.1.103: icmp_seq=85 ttl=128 time=0.773ms64 bytes from 192.168.1.103: icmp_seq=86 ttl=128 time=1.26ms^C--- 192.168.1.103 ping statistics ---
86 packets transmitted, 86 received, 0% packet loss, time85134ms
rtt min/avg/max/mdev = 0.467/1.051/4.810/0.535ms
打exit退出screen
[screen is terminating]
也可以不手動建立session,直接使用screen 指令執行要運作的指令,這樣在指令中的一切操作也都會被記錄下來,當指令執行結束後 screen 會話也會自動結束。
這種情況下,session名字是系統自動建立的。
[[email protected] ~]$ screen /opt/elasticsearch-6.5.1/bin/elasticsearch
斷開ssh後,重新檢視
[[email protected]~]$ screen -lsThere is a screen on:4104.pts-0.localhost (Detached)1 Socket in /var/run/screen/S-es.
用kill指令殺掉elasticsearch後,screen自動終止
2.3、共享session,可以多個終端連入同一個session,所有操作在連入的終端上都可以顯示,所有終端都可以操作。
[[email protected] network-scripts]# screen -lsThere are screens on:4769.ping3 (Detached)4704.myping (Detached)2 Sockets in /var/run/screen/S-root.
[[email protected] network-scripts]# screen -x ping3