screen - screen manager with VT100/ANSI terminal emulation (具有 VT100/ANSI 終端仿真的屏幕管理器)

screen - screen manager with VT100/ANSI terminal emulation (具有 VT100/ANSI 終端仿真的屏幕管理器)

1. screen [-opts] [cmd [args]] or screen -r [host.tty]

Options:
-4            Resolve hostnames only to IPv4 addresses.
-6            Resolve hostnames only to IPv6 addresses.
-a            Force all capabilities into each window's termcap.
-A -[r|R]     Adapt all windows to the new display width & height.
-c file       Read configuration file instead of '.screenrc'.
-d (-r)       Detach the elsewhere running screen (and reattach here).
-dmS name     Start as daemon: Screen session in detached mode.
-D (-r)       Detach and logout remote (and reattach here).
-D -RR        Do whatever is needed to get a screen session.
-e xy         Change command characters.
-f            Flow control on, -fn = off, -fa = auto.
-h lines      Set the size of the scrollback history buffer.
-i            Interrupt output sooner when flow control is on.
-l            Login mode on (update /var/run/utmp), -ln = off.
-ls [match]   or
-list         Do nothing, just list our SockDir [on possible matches].
-L            Turn on output logging.
-m            ignore $STY variable, do create a new screen session.
-O            Choose optimal output rather than exact vt100 emulation.
-p window     Preselect the named window if it exists.
-q            Quiet startup. Exits with non-zero return code if unsuccessful.
-Q            Commands will send the response to the stdout of the querying process.
-r [session]  Reattach to a detached screen process.
-R            Reattach if possible, otherwise start a new session.
-s shell      Shell to execute rather than $SHELL.
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title      Set title. (window's name).
-T term       Use term as $TERM for windows, rather than "screen".
-U            Tell screen to use UTF-8 encoding.
-v            Print "Screen version 4.03.01 (GNU) 28-Jun-15".
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-x            Attach to a not detached screen. (Multi display mode).
-X            Execute <cmd> as a screen command in the specified session.
resolve [rɪ'zɒlv]:vt. 決定,溶解,使...分解,決心要做... vi. 解決,決心,分解 n. 堅決,決定要做的事

遠程登錄到 Linux 服務器,經常需要很長時間才能完成任務,在此期間不能關掉窗口或者斷開連接,否則這個任務就會被殺掉。可以用 screen 命令解決這個問題。screen 命令可以實現當前窗口與任務分離,即使離線了,服務器仍在後臺運行任務。當重新登錄服務器,可以讀取窗口線程,重新連接任務窗口。只要 screen 本身沒有終止,在其內部運行的會話都可以恢復。這一點對於遠程登錄的用戶特別有用,即使網絡連接中斷,用戶也不會失去對已經打開的命令行會話的控制。screen 在網絡環境不好的地方非常的有用。

screen -v
-v - Print "Screen version 4.03.01 (GNU) 28-Jun-15".

strong@foreverstrong:~$ screen -v
Screen version 4.03.01 (GNU) 28-Jun-15
strong@foreverstrong:~$

screen -ls / screen -list
-ls [match] or -list - Do nothing, just list our SockDir [on possible matches].

strong@foreverstrong:~$ screen -ls
No Sockets found in /var/run/screen/S-strong.

strong@foreverstrong:~$
strong@foreverstrong:~$ screen -list
No Sockets found in /var/run/screen/S-strong.

strong@foreverstrong:~$

screen -S yong
-S sockname - Name this session <pid>.sockname instead of <pid>.<tty>.<host>.

新建名稱爲 yong 的 session,並進入 yong 的 session。

使用 screen -ls 查看當前存在的 session。Attached 表示這個 screen 會話是活動的。其中 3380.yong 中 3380 表示 screen 會話的進程號,yong 表示 screen 會話名。

strong@foreverstrong:~$ screen -S yong

strong@foreverstrong:~$ screen -ls
There is a screen on:
        3380.yong       (09/12/2019 07:27:09 PM)        (Attached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -list
There is a screen on:
        3380.yong       (09/12/2019 07:27:09 PM)        (Attached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$

Ctrl + A D -> detach
暫時離開當前 session,將目前的 screen session 丟到後臺執行,並回到還沒進 screen 時的狀態,此時在 screen session 裏運行的 process 會繼續執行,即使 logout 也不影響。

C-a d / C-a C-d - (detach) Detach screen from this terminal.

[detached from 3380.yong]
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -list
There is a screen on:
        3380.yong       (09/12/2019 07:27:09 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There is a screen on:
        3380.yong       (09/12/2019 07:27:09 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$

screen -r yong
-r [session] - Reattach to a detached screen process.

恢復離線的 screen 會話,回到會話名 yong。

detach [dɪ'tætʃ]:vt. 分離,派遣,使超然
attach [ə'tætʃ]:vt. 使依附,貼上,繫上,使依戀 vi. 附加,附屬,伴隨
strong@foreverstrong:~$ screen -ls
There is a screen on:
        3380.yong       (09/12/2019 07:27:08 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -r yong

......

strong@foreverstrong:~$ screen -list
There is a screen on:
        3380.yong       (09/12/2019 07:27:08 PM)        (Attached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$

screen -x 3380
-x - Attach to a not detached screen. (Multi display mode).

screen -x / screen -r + 進程 ID 或者會話名都可以。恢復離線的 screen 作業。

strong@foreverstrong:~$ screen -ls
There is a screen on:
        3380.yong       (09/12/2019 07:27:08 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -x 3380

......

strong@foreverstrong:~$ screen -list
There is a screen on:
        3380.yong       (09/12/2019 07:27:09 PM)        (Attached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$

新建一個名稱爲 qiang 的會話,並進入到該會話中。

strong@foreverstrong:~$ screen -S qiang

列出所有會話列表。

strong@foreverstrong:~$ screen -ls
There are screens on:
        7042.qiang      (09/13/2019 05:01:37 PM)        (Attached)
        3380.yong       (09/12/2019 07:27:08 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$

kill 3380
kill 3380 殺死 3380.yong。

strong@foreverstrong:~$ screen -ls
There are screens on:
        7042.qiang      (09/13/2019 05:01:37 PM)        (Detached)
        3380.yong       (09/12/2019 07:27:08 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ kill 3380
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There is a screen on:
        7042.qiang      (09/13/2019 05:01:38 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$

screen -dmS cheng
-dmS name - Start as daemon: Screen session in detached mode.

新建一個名稱爲 cheng 的會話,不進入到該會話中。

daemon ['diːmən]:n. 守護進程,後臺程序
strong@foreverstrong:~$ screen -ls
There is a screen on:
        7042.qiang      (09/13/2019 05:01:37 PM)        (Detached)
1 Socket in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -dmS cheng
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:36 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$

screen -X -S 16019 quit
screen -X -S 16019 quit 關閉 16019.test。

strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:36 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -dmS test
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There are screens on:
        16019.test      (09/16/2019 08:23:46 AM)        (Detached)
        15991.cheng     (09/16/2019 08:17:36 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Detached)
3 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -X -S 16019 quit
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:36 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$

screen -wipe
-wipe [match] Do nothing, just clean up SockDir [on possible matches].

screen -list 會顯示會話爲 dead 狀態。使用 screen -wipe 命令清除該會話

screen -d 7042
-d (-r) - Detach the elsewhere running screen (and reattach here).

strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:36 AM)        (Attached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Attached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -d 15991

......

[remote detached from 15991.cheng]
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:36 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:38 PM)        (Attached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
strong@foreverstrong:~$ screen -d 7042
[7042.qiang detached.]

strong@foreverstrong:~$
strong@foreverstrong:~$ screen -ls
There are screens on:
        15991.cheng     (09/16/2019 08:17:35 AM)        (Detached)
        7042.qiang      (09/13/2019 05:01:37 PM)        (Detached)
2 Sockets in /var/run/screen/S-strong.
strong@foreverstrong:~$
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章