文章目錄
- (E) But how do I switch between these two tasks?
- Common screen commands
linux中screen命令的用法
From: http://www.9usb.net/201002/linux-screen-mingling.html
作為linux伺服器管理員,經常要使用ssh登陸到遠程linux機器上做一些耗時的操作。
也許你遇到過使用telnet或SSH遠程登入linux,運行一些程式。如果這些程式需要運行很長時間(幾個小時),而程式運行過程中出現網路故障,或者客戶機故障,這時候客戶機與遠程伺服器的連結將終端,並且遠程伺服器沒有正常結束的命令將被迫終止。
又比如你SSH到主機上後,開始批量的scp命令,如果這個ssh線程斷線了,scp進程就中斷了。在遠程伺服器上正在運行某些耗時的作業,但是工作還沒做完快要下班了,退出的話就會中斷操作了,如何才好呢?
我們利用screen命令可以很好的解決這個問題。實現在斷開SSH的情況下,在伺服器上繼續執行程式。
那什麼是screen命令?
Screen被稱之為一個全屏視窗管理器,用他可以輕鬆在一個物理終端上獲得多個虛擬終端的效果。
Screen功能說明:
簡單來說,Screen是一個可以在多個進程之間多工一個物理終端的視窗管理器,這意味著你能夠使用一個單一的終端視窗運行多終端的應用。Screen中有會話的概念,使用者可以在一個screen會話中建立多個screen視窗,在每一個screen視窗中就像操作一個真實的telnet/SSH串連視窗那樣。
Screen命令文法:
screen [-AmRvx -ls -wipe][-d <作業名稱>][-h <行數>][-r <作業名稱>][-s ][-S <作業名稱>]
Screen命令參數:
-A -[r|R] 將所有的視窗都調整為目前終端機的大小。
-c filename 用指定的filename檔案替代screen的設定檔’.screenrc’.
-d [pid.tty.host] 斷開screen進程(使用該命令時,screen的狀態一定要是Attached,也就是說有使用者連在screen裡)。一般進程的名字是以pid.tty.host這種形式表示(用screen -list命令可以看出狀態)。
-D [pid.tty.host] 與-d命令實現一樣的功能,區別就是如果執行成功,會踢掉原來在screen裡的使用者並讓他logout。
-h <行數> 指定視窗的緩衝區行數。
-ls或–list 顯示目前所有的screen作業。
-m 即使目前已在作業中的screen作業,仍強制建立新的screen作業。
-p number or name 預先選擇一個視窗。
-r [pid.tty.host] 恢複離線的screen進程,如果有多個斷開的進程,需要指定[pid.tty.host]
-R 先試圖恢複離線的作業。若找不到離線的作業,即建立新的screen作業。
-s shell 指定建立新視窗時,所要執行的shell。
-S <作業名稱> 指定screen作業的名稱。(用來替代[pid.tty.host]的命名方式,可以簡化操作).
-v 顯示版本資訊。
-wipe 檢查目前所有的screen作業,並刪除已經無法使用的screen作業。
-x 恢複之前離線的screen作業。
Screen命令的常規用法:
screen -d -r:串連一個screen進程,如果該進程是attached,就先踢掉遠端使用者再串連。
screen -D -r:串連一個screen進程,如果該進程是attached,就先踢掉遠端使用者並讓他logout再串連
screen -ls或者-list:顯示存在的screen進程,常用命令
screen -m:如果在一個Screen進程裡,用快速鍵crtl+a c或者直接打screen可以建立一個新視窗,screen -m可以建立一個screen進程。
screen -dm:建立一個screen,並預設是detached模式,也就是建好之後不會連上去。
screen -p number or name:預先選擇一個視窗。
Screen實現後台運行程式的簡單步驟:
1> 要進行某項操作時,先使用命令建立一個Screen:
[linux@user~]$ screen -S test1
2>接著就可以在裡面進行操作了,如果你的任務還沒完成就要走開的話,使用命令保留Screen:
[linux@user~]$ Ctrl+a+d #按Ctrl+a,然後再按d即可保留Screen
[detached] #這時會顯示出這個提示,說明已經保留好Screen了
如果你工作完成的話,就直接輸入:
[linux@user~]$ exit #這樣就表示成功退出了
[screen is terminating]
3> 如果你上一次保留了Screen,可以使用命令查看:
[linux@user~]$ screen -ls
There is a screen on:
9649.test1 (Detached)
恢複Screen,使用命令:
[linux@user~]$ screen -r test1 (or 9649)
Screen命令中用到的快速鍵
Ctrl+a c :建立視窗
Ctrl+a w :視窗列表
Ctrl+a n :下一個視窗
Ctrl+a p :上一個視窗
Ctrl+a 0-9 :在第0個視窗和第9個視窗之間切換
Ctrl+a K(大寫) :關閉當前視窗,並且切換到下一個視窗(當退出最後一個視窗時,該終端自動終止,並且退回到原始shell狀態)
exit :關閉當前視窗,並且切換到下一個視窗(當退出最後一個視窗時,該終端自動終止,並且退回到原始shell狀態)
Ctrl+a d :退出當前終端,返回載入screen前的shell命令狀態
Refer:
http://www.cyberciti.biz/tips/linux-screen-command-howto.html
How do I Use Multiple Screens on One Terminal over ssh session?
by nixCraft on
December 7, 2005 · 22 comments·
LAST UPDATED January 24, 2009
in CentOS,
Debian Linux,
Gentoo Linux
Most of the time GUI is not available on remote Linux system, you login over ssh and start to work, if you need to run two or three task at a time, you login over ssh two or three times. However, with screen windows manager utility you can run multiple terminals
at the same time from single console login over ssh session (UNIX guru uses the term called multiplexing for this concept). Any seasoned Linux admin should be aware of this nifty tool
Install screen if it is not installed under Debian Linux
Type the following command:
# apt-get install screen
FreeBSD user can use ports to install screen
# cd /usr/ports/misc/screen
# make install clean
Login to remote server over ssh
$ ssh me@myserver.com
(C) Start screen session. You can name your window/session (1 is name of session):
$ screen -S 1
Let us start pine mail reader on first session or window.
$ pine
(D) Next you would like to download something from ftp/http site while you are reading emails. You need to create another screen window by pressing special key combination. Press CTRL + a followed by c key (first hit CTRL+a, releases both keys and press
c). As soon as you hit 'c' you will get new shell prompt at console. Now use wget to download belenix.iso.bz2 (Solaris live CD) from net:
$ wget http://www.genunix.org/distributions/belenix_site/binfiles/belenix.iso.bz2
(E) But how do I switch between these two tasks?
- Switching between windows is the specialty of screen utility. So to switch between pine and wget window (or session) press CTRL+a followed by n key (first hit CTRL+a, releases both keys and press n).
- To list all windows use the command CTRL+a followed by " key (first hit CTRL+a, releases both keys and press " ).
- To switch to window by number use the command CTRL+a followed by ' (first hit CTRL+a, releases both keys and press ' it will prompt for window number).
Common screen commands
| screen command |
Task |
| Ctrl+a c |
Create new window |
| Ctrl+a k |
Kill the current window / session |
| Ctrl+a w |
List all windows |
| Ctrl+a 0-9 |
Go to a window numbered 0 9, use Ctrl+a w to see number |
| Ctrl+a Ctrl+a |
Toggle / switch between the current and previous window |
| Ctrl+a S |
Split terminal horizontally into regions and press Ctrl+a c to create new window there |
| Ctrl+a :resize |
Resize region |
| Ctrl+a :fit |
Fit screen size to new terminal size. You can also hit Ctrl+a F for the the same task |
| Ctrl+a :remove |
Remove / delete region. You can also hit Ctrl+a X for the same taks |
| Ctrl+a tab |
Move to next region |
| Ctrl+a D (Shift-d) |
Power detach and logout |
| Ctrl+a d |
Detach but keep shell window open |
| Ctrl-a Ctrl-\ |
Quit screen |
| Ctrl-a ? |
Display help screen i.e. display a list of commands |
Suggested readings:
See screen command man page for further details:
man screen