---
title: screen | zh
tags: Guide, TWNIA3, TW
GA:
---
{%hackmd @docsharedstyle/default %}
# screen
用 [**SSH** 登入**台灣杉三號**](https://man.twcc.ai/@TWCC-III-manual/Bk-pE62jd)後可以開啓 screen 進行多個互動式的 shell,避免因意外操作造成程式執行中斷或檔案傳輸失敗。
操作過程中可以暫時卸離(detach),需要使用時再重新連接(attach)即可繼續操作。
---
### 常用指令介紹
:::warning
<i class="fa fa-lightbulb-o" aria-hidden="true"></i> **說明:**
若需要查詢更多詳細指令 (分隔畫面、多方分享...等),在登入 <b>screen</b> 介面後輸入 `man screen` 和 `info screen` 查詢相關指令。也可以從 <b>[Screen User's Manual](https://www.gnu.org/software/screen/manual/screen.html#Selecting)</b> 進行了解。
:::
- 開啟 <b>screen</b>
輸入 `screen` 開啟進入畫面,按下 <b>Space</b> 進入 <b>Screen</b> 模式
```
xxx@HYdeMacBook-Air Desktop % screen
```

- 開啟新的 <b>screen session</b>
輸入 `screen -S (session_name)`
```
xxx@HYdeMacBook-Air Desktop % screen -S new_session
```
- 重新載入之前開啟的 <b>screen</b>
輸入 `screen -r (screen ID)`
```
xxx@HYdeMacBook-Air Desktop % screen -r
There are several suitable screens on:
11618.myFirst_screen (Attached)
11607.ttys001.xxxdeMacBook-Air (Attached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
xxx@HYdeMacBook-Air Desktop %
```
- <b>detached screen</b> 回到 terminal (<b>screen</b> 仍在背景執行)
使用 <b>ctrl</b>+<b>C</b>
```
xxx@HYdeMacBook-Air Desktop % ./longtask
Doing stuff...
Doing stuff...
Doing stuff...
Doing stuff...
Doing stuff...
Doing stuff...
Doing stuff...
Doing stuff...
```
```
xxx@HYdeMacBook-Air Desktop % screen
[detached]
```
- 列出在背景程式執行的項目
輸入`screen -ls`
```
xxx@HYdeMacBook-Air Desktop % screen -ls
There are screens on:
19125.ttys004.xxxdeMacBook-Air (Detached)
18852.new_long_task (Attached)
18621.ttys004.xxxdeMacBook-Air (Detached)
19335.very_long_task (Attached)
18323.ttys004.xxxdeMacBook-Air (Detached)
5 Sockets in /var/folders/z4/w961r3ns5h3bkxc2v7575cqc0000gn/T/.screen.
```
- 顯示程式相關的全部資訊
輸入 `ps aux | grep (screen Name)`
```
xxx@HYdeMacBook-Air Desktop % ps aux | grep longtask
xxx 16472 0.0 0.0 407962768 672 s001 R+ 9:57上午 0:00.00 grep longtask
xxx 16387 0.0 0.0 408106960 1904 s012 S+ 9:56上午 0:00.07 /bin/bash ./longtask
```
- 刪除指定資訊
輸入 `screen -X -S (screen ID) quit`
```
xxx@HYdeMacBook-Air Desktop % screen -ls
There are screens on:
17813.ttys001.xxxdeMacBook-Air (Attached)
11829.ttys001.xxxdeMacBook-Air (Detached)
2 Sockets in /var/folders/z4/w961r3ns5h3bkxc2v7575cqc0000gn/T/.screen.
```
```
xxx@HYdeMacBook-Air Desktop % screen -X -S 11829 quit
xxx@HYdeMacBook-Air Desktop % screen -ls
There is a screen on:
17813.ttys001.xxxdeMacBook-Air (Attached)
```
- 關閉 <b>screen</b>
輸入 `exit`
```
h.y_lee@wrl-tc-171 ~ % screen
[screen is terminating]
h.y_lee@wrl-tc-171 ~ %
```