快轉到主要內容

"Screen" Command 讓程式在背景執行,並隨意切換,即便登出也不會中斷!

·939 字
Computer-Science SSH
目錄

What is “screen” Command
#

這個指令可以於背景創造多個終端 Session,用於多線程的操作,並且可以輕易的在各個 Session 之間切換,且在用戶登出後,Session 仍可在背景運作。


Basic Using
#

以下是常見的參數:

  • -ls 觀看存在背景的
  • -r [id] 回復互動視窗,指定 ID [id],若背景只有一個 session 則不用指定。
  • -S [n] 指定一個名為 [n] 且已存在的 session,若不存在則創建一個 session 並指定名稱 [n]。(也可以不指定 [n])
  • -x [id] Attach 一個 session,與-r有些相似,但可以多人使用。
  • -X [cmd] 在創建 session 時指定在該 session 啟動完畢後欲執行的指令。也可以搭配-S指定要執行指令的 session。
  • -d [id] Detach session,用於將正在使用-r互動的視窗給移至背景(正在被使用-r的視窗是無法被其他使用者再次-r的,因此需要先-d)。該操作可遠端執行,可以指定 Session ID [id]。
  • -rd [id] 回復視窗之前 detach 其他視窗,等同於先執行-d後執行-r
  • -d -m, -dm Detach mode, 於 detach 模式下啟動一個 session。

先創建 session 後按下 Ctrl+a d 可以將 session detach。


Example
#

創建名為「example」的 Session
#

1screen -S example

查看當前存在的 Session
#

1$ screen -ls
2There is a screen on:
3  43434.example (09/17/22 15:06:45) (Attached)
41 Socket in /run/screen/S-Bob.

若是該 session 生命週期已經結束,則會被標示「dead」,使用參數-wipe可以移除這些 sessions:

1$ screen -wipe
2There is a screen on:
3  43434.example (09/17/22 15:06:45) (Attached)
41 Socket in /run/screen/S-Bob.

Detach Session (背景運作)
#

按下 Ctrl+a 後按下 d

Attach 「example」 Session(互動 Session)
#

1screen -r example

若是顯示「There is no screen to be resumed matching example.」則表示該 session 目前處於attached狀態。

該情況需要先進行detach再使用-r回復,或是一起執行:

1screen -rd example

或者使用-x的方式:

1screen -x example

若是多人用戶的模式下,可以使用<user>/<screen>來指定該使用者的 session,例如使用者名為「Bob」,並該使用者有著一個 session 名為「example」,則可以:

1screen -x Bob/example

退出 Session
#

可以透過以下方式來將指定的 session 退出,舉例要退出「example」session:

1screen -X -S example quit

多人模式設定
#

以下指令執行於按下 Ctrl+a 後直接輸入:

:multiuser on:開啟多人使用模式(允許其他使用者進入 session)。
:multiuser off:關閉多人使用模式。
:acladd <user>:多人使用模式下,允許使用該 session 的使用者白名單。
:acldel <user>:多人使用模式下,移除可使用該 session 的使用者。

Alpaca
作者
Alpaca
No one can stop my feet.

相關文章

Simply setting up a Proxy Server with SSH
·344 字
Computer-Science SSH

Did you know that SSH can make your host as a proxy server?

How to set timezone on ubuntu?
·183 字
Computer-Science Ubuntu
PHP MySQL query with 「PDO」
·332 字
Computer-Science PHP SQL PDO MySQL

Don’t continue to use original MySQL functions in PHP, let’s try PDO !

How to use FFmpeg ?
·308 字
Computer-Science FFmpeg
Ubuntu 更改家目錄語言
·574 字
Computer-Science Ubuntu

簡單幾步變更家目錄的語言

CTF 「hackme」Web Write Up
·17858 字
Computer-Science CTF