Linux之screen命令使用技巧

先來看看Screen的官方說明:
GNU's Screen 官方站點:http://www.gnu.org/software/screen/

簡單來講Screen 個人來說主要用於執行長時間不能中斷的備份、測試和傳輸任務等(恢復會話);以及共享終端會話窗口(窗口共享)

使用 screen -S <會話名> 創建並進入screen會話,例如:

[root@centos7 11:11:29 ~]#screen -ls
No Sockets found in /var/run/screen/S-root.

[root@centos7 11:11:35 ~]#screen -S zmh
[root@centos7 11:11:40 ~]#screen -ls
There is a screen on:
        4705.zmh        (Attached)
1 Socket in /var/run/screen/S-root.

這裏創建並進入名叫“zmh”的screen作業,再使用screen -ls查看當前存在的screen會。“Attached”表示這個screen會話是活動的(個人理解爲有人接入)。其中“3891.zmh” 3891表示此screen會話的進程號,zmh表示screen會話名。
在我們使用ctrl+a,d(同時按ctrl 和 a 再鬆手按d)剝離screen會話後,可以使用以下方法再次接入screen會話:

[root@centos7 11:33:12 ~]#screen -ls
There is a screen on:
    4705.zmh    (Detached)
1 Socket in /var/run/screen/S-root.

[root@centos7 11:33:18 ~]#screen -x 4705

或者:

[root@centos7 11:34:17 ~]#screen -ls
There is a screen on:
    4705.zmh    (Detached)
1 Socket in /var/run/screen/S-root.

[root@centos7 11:34:20 ~]#screen -r zmh

兩種方法後面參數分別可以跟上進程或者會話名都可以

1.利用screen來執行一個長時間任務,這裏使用ping來測試:

[root@centos7 11:50:21 ~]#screen -x zmh
[root@centos7 11:54:25 ~]#
[root@centos7 11:54:25 ~]#screen -ls
There is a screen on:
    4705.zmh    (Attached)
1 Socket in /var/run/screen/S-root.

[root@centos7 11:54:27 ~]#ping 192.168.30.1
PING 192.168.30.1 (192.168.30.1) 56(84) bytes of data.
64 bytes from 192.168.30.1: icmp_seq=1 ttl=128 time=0.362 ms
64 bytes from 192.168.30.1: icmp_seq=2 ttl=128 time=1.46 ms
64 bytes from 192.168.30.1: icmp_seq=3 ttl=128 time=1.26 ms
^A64 bytes from 192.168.30.1: icmp_seq=4 ttl=128 time=1.00 ms
d64 bytes from 192.168.30.1: icmp_seq=5 ttl=128 time=1.35 ms
^C
--- 192.168.30.1 ping statistics ---
6 packets transmitted, 5 received, 16% packet loss, time 5021ms
rtt min/avg/max/mdev = 0.362/1.090/1.465/0.394 ms

然後ctrl+a,d退出,並logout終端:

[root@centos7 12:13:09 ~]#screen -ls
There is a screen on:
    4705.zmh    (Detached)
1 Socket in /var/run/screen/S-root.
[root@centos7 12:13:13 ~]#
[root@centos7 12:14:40 ~]#exit
logout
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.30.103_Cent7.4) at 12:17:45.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

Connecting to 192.168.30.103:22...   *<<<<<<<<<<重新登錄*
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Sun Apr  1 11:39:36 2018 from 192.168.30.1
[root@centos7 12:15:15 ~]#screen -x zmh
[detached from 4705.zmh]
[root@centos7 12:15:23 ~]#screen -x zmh

64 bytes from 192.168.30.1: icmp_seq=20 ttl=128 time=1.43 ms
64 bytes from 192.168.30.1: icmp_seq=21 ttl=128 time=1.08 ms
64 bytes from 192.168.30.1: icmp_seq=22 ttl=128 time=1.19 ms
64 bytes from 192.168.30.1: icmp_seq=23 ttl=128 time=1.73 ms
64 bytes from 192.168.30.1: icmp_seq=24 ttl=128 time=1.23 ms
64 bytes from 192.168.30.1: icmp_seq=25 ttl=128 time=0.635 ms

可以看到screen會話ping任務仍然在執行未中斷。

這裏還有個疑問:其他系統用戶能夠接入此screen會話嗎?

[root@centos7 12:29:23 ~]#screen -ls
There is a screen on:
        4705.zmh        (Attached)
1 Socket in **/var/run/screen/S-root**.

[root@centos7 12:30:01 ~]#

<<<<<<以下是zmh用戶的screen狀態>>>>>>>>>>>

[zmh@centos7 12:30:12 ~]$screen -ls
No Sockets found in **/var/run/screen/S-zmh**.

[zmh@centos7 12:30:14 ~]$

顯然其他用戶看不到root用戶創建的screen 會話。綠色部分可以看出每個用戶各自的screen會話路徑信息不同。在創建一個zmh用戶下的screen作業後,進一步查看此路徑可以看到,每個screen作業會打開一個socket文件

[root@centos7 12:36:50 ~]#ll /var/run/screen/S-zmh/
total 0
srwx------. 1 zmh zmh 0 Apr  1 12:36 5923.screentest
[root@centos7 12:39:16 ~]#ll /var/run/screen/S-root/
total 0
srwx------. 1 root root 0 Apr  1 12:15 4705.zmh
[root@centos7 12:39:22 ~]#

因此這裏也提供另一種關閉screen會話的方式,即刪除這些socket文件

[root@centos7 12:39:22 ~]#rm -f /var/run/screen/S-zmh/5923.screentest 
[root@centos7 12:42:11 ~]#rm -f /var/run/screen/S-root/4705.zmh 
[root@centos7 12:42:37 ~]#screen -ls
No Sockets found in /var/run/screen/S-root.

[root@centos7 12:42:39 ~]#

2.Screen的另一個比較實用的功能是能夠多終端會話共享,以下是使用"screen -S ZMH"開啓一個叫ZMH的screen作業,並在兩個終端使用"screen -x ZMH"命令同時接入作業實現終端打印共享:
Linux之screen命令使用技巧

因爲screen會話能夠共享因此在自己使用screen作業時小心有人來監視你喲,我們可以在作業中使用ctrl+a, * 來查看當前screen有多少個終端接入:

term-type   size         user interface           window       Perms
---------- ------- ---------- ----------------- ----------     -----
 xterm       74x37        root@/dev/pts/1          0(root@cento) rwx
 xterm       74x37        root@/dev/pts/0          0(root@cento) rwx

以上可以看到有pts/0和pts/1兩個終端接入此screen作業。此時可以使用w命令看看系統登錄情況:

[root@centos7 13:01:16 ~]#w
 13:02:56 up  2:58,  5 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/4    192.168.30.1     12:54    0.00s  0.05s  0.00s w
root     pts/5    192.168.30.1:S.0 12:55    8.00s  0.10s  0.10s /bin/bash
[root@centos7 13:02:56 ~]#

這裏雖然只看到兩個TTY登錄,其中pts/5 的FROM IP,192.68.30.1:S.0後面有:S表示該TTY是screen作業,在本實驗中就隱藏着兩個登錄TTY終端。
分別在兩個接入screen作業中退出ctrl+a,d 再用w命令查看登錄情況,可以看到screen作業佔用的TTY終端已經釋放,隱藏的連個傢伙現身了:

[root@centos7 13:02:56 ~]#w
 13:06:32 up  3:02,  6 users,  load average: 0.03, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/1    192.168.30.1     12:54   16.00s  0.09s  0.09s -bash
root     pts/0    192.168.30.1     12:15    8.00s  0.13s  0.13s -bash
root     pts/4    192.168.30.1     12:54    0.00s  0.05s  0.00s w
[root@centos7 13:06:32 ~]#

另外,在screen作業中,使用ctrl+a,? 可以查看還有很多快捷操作等待我們慢慢探索。

Linux之screen命令使用技巧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章