1. 程式人生 > 實用技巧 >linux系統中不間斷會話服務screen命令

linux系統中不間斷會話服務screen命令

screen命令解決的問題:

使用者可以通過該軟體同時連線多個本地或遠端的命令列會話,並在其間自由切換

如果一些程式需要執行很長時間,而程式執行過程中出現網路故障,或者客戶機故障,這時候客戶機與遠端伺服器的連結將終端,並且遠端伺服器沒有正常結束的命令將被迫終止(可以使用screen避免這種情況)。

即便網路中斷會話也可以恢復

基本用法:

1、建立screen視窗,screen -S name

[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# pwd
/root/test
[root@linuxprobe test]# screen 
-S test1 ## 沒有安裝screen -bash: /usr/bin/screen: No such file or directory

使用yum安裝screen,前提是已經配置好yum倉庫。

[root@linuxprobe test]# yum install screen -y

[root@linuxprobe test]# screen --version   ## 已安裝成功
Screen version 4.01.00devel (GNU) 2-May-06
[root@linuxprobe test]# which screen
/usr/bin/screen

建立一個screen視窗:

[root@linuxprobe test]# screen -S test1  ## 建立視窗
[root@linuxprobe test]# exit  ## 退出screen視窗或者ctrl a + d(放置後臺)

2、建立scrren視窗,執行程式,然後放置後臺,看能否恢復視窗

[root@linuxprobe test]# screen -S test1  ## 建立名為test1的視窗
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > a.txt  ## 執行一個耗時程式
ctrl a + d ## 先按ctrl和a,再按d,將該視窗放置後臺

3、如何檢視已經建立的視窗,比如剛才建立的test1視窗

[root@linuxprobe test]# screen -ls
There is a screen on:
        2648.test1     (Detached)
1 Socket in /var/run/screen/S-root.

4、再建立一個screen視窗,執行耗時程式,放置後臺

[root@linuxprobe test]# screen -S test2  ## 建立一個名為test2的screen視窗
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > b.txt ## 在該視窗內執行一個耗時程式
ctrl a + d ## 先按ctrl 和a,再按d,將該screen放置後臺

5、檢視當前提交了多少個screen

[root@linuxprobe test]# screen -ls  ## 可見一共遞交了兩個
There are screens on:
        2688.test2     (Detached)
        2648.test1     (Detached)
2 Sockets in /var/run/screen/S-root.

6、恢復其中一個screen視窗,檢視遞交的程式

[root@linuxprobe test]# seq 300000000 | sort | uniq -c > a.txt

7、退出當前的終端,再次登入驗證程式是否還在

## 再次登入後,仍然存在
[root@linuxprobe ~]# screen -ls There are screens on: 2688.test2 (Detached) 2648.test1 (Detached) 2 Sockets in /var/run/screen/S-root. [root@linuxprobe ~]# screen -r 32986.test2 [root@linuxprobe test]# seq 300000000 | sort | uniq -c > b.txt

8、如何關閉screen視窗

[root@linuxprobe test]# screen -ls  ## 檢視當前視窗
There are screens on:
        2688.test2      (Detached)
        2648.test1      (Detached)
2 Sockets in /var/run/screen/S-root.

[root@linuxprobe test]# screen -X -S 2648.test1 quit ## 關閉視窗1
[root@linuxprobe test]# screen -ls
There is a screen on:
        2688.test2      (Detached)
1 Socket in /var/run/screen/S-root.

[root@linuxprobe test]# kill 2688  ## 可以直接殺掉,關閉test2
[root@linuxprobe test]# screen -ls
No Sockets found in /var/run/screen/S-root.

9、重新建立視窗,斷網,檢測程式是否會斷

[root@linuxprobe test]# screen -S test
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > c.txt
ctrl a + d ## 放置後臺
[root@linuxprobe test]# screen -ls
There is a screen on:
        2993.test       (Detached)
1 Socket in /var/run/screen/S-root.

斷網:

測試網路連線:

[root@linuxprobe test]# ping -c 3 www.baidu.com
ping: unknown host www.baidu.com

30s後開啟:

測試網路連線:

[root@linuxprobe test]# ping -c 3 www.baidu.com
PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=16.7 ms
64 bytes from 39.156.66.14: icmp_seq=2 ttl=51 time=16.5 ms
64 bytes from 39.

檢查程式,程式依然執行

[root@linuxprobe test]# screen -ls
There is a screen on:
    2993.test    (Detached)
1 Socket in /var/run/screen/S-root.

[root@linuxprobe test]# screen -r 2993.test
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > c.txt