我們經常會碰到這樣的問題,用telnet/ssh登錄了遠程的Linux服務器,運行了一些耗時較長的任務,結果卻由於網絡的不穩定導致任務中途失敗。如何讓命令提交後不受本地關閉終端窗口/網絡斷開連接的干擾呢?下面舉了一些例子,您可以針對不同的場景選擇不同的方式來處理這個問題。
如果只是臨時有一個命令需要長時間運行,什麼方法能最簡便的保證它在後臺穩定運行呢?
我們知道,當用戶註銷(logout)或者網絡斷開時,終端會收到HUP(hangup)信號從而關閉其所有子進程。因此,我們的解決辦法就有兩種途徑:要麼讓進程忽略HUP信號,要麼讓進程運行在新的會話裏從而成爲不屬於此終端的子進程。
1.nohup
nohup無疑是我們首先想到的辦法。顧名思義,nohup的用途就是讓提交的命令忽略hangup信號。讓我們先來看一下nohup的幫助信息:
NOHUP(1) User Commands NOHUP(1) NAME nohup - run a command immune to hangups, with output to a non-tty SYNOPSIS nohup COMMAND [ARG]... nohup OPTION DESCRIPTION Run COMMAND, ignoring hangup signals. --help display this help and exit --version output version information and exit |
可見,nohup的使用是十分方便的,只需在要處理的命令前加上nohup即可,標準輸出和標準錯誤缺省會被重定向到nohup.out文件中。一般我們可在結尾加上"&"來將命令同時放入後臺運行,也可用">filename2>&1"
來更改缺省的重定向文件名。
[root@pvcent107 ~]# nohup ping www.ibm.com & [1] 3059 nohup: appending output to `nohup.out' [root@pvcent107 ~]# ps -ef |grep 3059 root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.com root 3067 984 0 21:06 pts/3 00:00:00 grep 3059 [root@pvcent107 ~]# |
2。setsid
nohup無疑能通過忽略HUP信號來使我們的進程避免中途被中斷,但如果我們換個角度思考,如果我們的進程不屬於接受HUP信號的終端的子進程,那麼自然也就不會受到HUP信號的影響了。setsid就能幫助我們做到這一點。讓我們先來看一下setsid的幫助信息:
SETSID(8) Linux Programmer’s Manual SETSID(8) NAME setsid - run a program in a new session SYNOPSIS setsid program [ arg ... ] DESCRIPTION setsid runs a program in a new session. |
可見setsid的使用也是非常方便的,也只需在要處理的命令前加上setsid即可。
[root@pvcent107 ~]# setsid ping www.ibm.com [root@pvcent107 ~]# ps -ef |grep www.ibm.com root 31094 1 0 07:28 ? 00:00:00 ping www.ibm.com root 31102 29217 0 07:29 pts/4 00:00:00 grep www.ibm.com [root@pvcent107 ~]# |
值得注意的是,上例中我們的進程ID(PID)爲31094,而它的父ID(PPID)爲1(即爲init進程ID),並不是當前終端的進程ID。請將此例與nohup例中的父ID做比較。
3。&
這裏還有一個關於subshell的小技巧。我們知道,將一個或多個命名包含在“()”中就能讓這些命令在子shell中運行中,從而擴展出很多有趣的功能,我們現在要討論的就是其中之一。
當我們將"&"也放入“()”內之後,我們就會發現所提交的作業並不在作業列表中,也就是說,是無法通過jobs
來查看的。讓我們來看看爲什麼這樣就能躲過HUP信號的影響吧。
[root@pvcent107 ~]# (ping www.ibm.com &) [root@pvcent107 ~]# ps -ef |grep www.ibm.com root 16270 1 0 14:13 pts/4 00:00:00 ping www.ibm.com root 16278 15362 0 14:13 pts/4 00:00:00 grep www.ibm.com [root@pvcent107 ~]# |
從上例中可以看出,新提交的進程的父ID(PPID)爲1(init進程的PID),並不是當前終端的進程ID。因此並不屬於當前終端的子進程,從而也就不會受到當前終端的HUP信號的影響了。
我們已經知道,如果事先在命令前加上nohup或者setsid就可以避免HUP信號的影響。但是如果我們未加任何處理就已經提交了命令,該如何補救才能讓它避免HUP信號的影響呢?
這時想加nohup或者setsid已經爲時已晚,只能通過作業調度和disown來解決這個問題了。讓我們來看一下disown的幫助信息:
disown [-ar] [-h] [jobspec ...] Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job. |
可以看出,我們可以用如下方式來達成我們的目的。
用
disown-hjobspec
來使某個作業忽略HUP信號。用
disown-ah
來使所有的作業都忽略HUP信號。用
disown-rh
來使正在運行的作業忽略HUP信號。
需要注意的是,當使用過disown之後,會將把目標作業從作業列表中移除,我們將不能再使用jobs
來查看它,但是依然能夠用ps-ef
查找到它。
但是還有一個問題,這種方法的操作對象是作業,如果我們在運行命令時在結尾加了"&"來使它成爲一個作業並在後臺運行,那麼就萬事大吉了,我們可以通過jobs
命令來得到所有作業的列表。但是如果並沒有把當前命令作爲作業來運行,如何才能得到它的作業號呢?答案就是用CTRL-z(按住Ctrl鍵的同時按住z鍵)了!
CTRL-z的用途就是將當前進程掛起(Suspend),然後我們就可以用jobs
命令來查詢它的作業號,再用bgjobspec
來將它放入後臺並繼續運行。需要注意的是,如果掛起會影響當前進程的運行結果,請慎用此方法。
disown示例1(如果提交命令時已經用“&”將命令放入後臺運行,則可以直接使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile & [1] 4825 [root@pvcent107 build]# jobs [1]+ Running cp -i -r testLargeFile largeFile & [root@pvcent107 build]# disown -h %1 [root@pvcent107 build]# ps -ef |grep largeFile root 4825 968 1 09:46 pts/4 00:00:00 cp -i -r testLargeFile largeFile root 4853 968 0 09:46 pts/4 00:00:00 grep largeFile [root@pvcent107 build]# logout |
disown示例2(如果提交命令時未使用“&”將命令放入後臺運行,可使用CTRL-z和“bg”將其放入後臺,再使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile2 [1]+ Stopped cp -i -r testLargeFile largeFile2 [root@pvcent107 build]# bg %1 [1]+ cp -i -r testLargeFile largeFile2 & [root@pvcent107 build]# jobs [1]+ Running cp -i -r testLargeFile largeFile2 & [root@pvcent107 build]# disown -h %1 [root@pvcent107 build]# ps -ef |grep largeFile2 root 5790 5577 1 10:04 pts/3 00:00:00 cp -i -r testLargeFile largeFile2 root 5824 5577 0 10:05 pts/3 00:00:00 grep largeFile2 [root@pvcent107 build]# |
我們已經知道了如何讓進程免受HUP信號的影響,但是如果有大量這種命令需要在穩定的後臺裏運行,如何避免對每條命令都做這樣的操作呢?
此時最方便的方法就是screen了。簡單的說,screen提供了ANSI/VT100的終端模擬器,使它能夠在一個真實終端下運行多個全屏的僞終端。screen的參數很多,具有很強大的功能,我們在此僅介紹其常用功能以及簡要分析一下爲什麼使用screen能夠避免HUP信號的影響。我們先看一下screen的幫助信息:
SCREEN(1) SCREEN(1) NAME screen - screen manager with VT100/ANSI terminal emulation SYNOPSIS screen [ -options ] [ cmd [ args ] ] screen -r [[pid.]tty[.host]] screen -r sessionowner/[[pid.]tty[.host]] DESCRIPTION Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows. |
使用screen很方便,有以下幾個常用選項:
用
screen-dmSsessionname
來建立一個處於斷開模式下的會話(並指定其會話名)。用
screen-list
來列出所有會話。用
screen-rsessionname
來重新連接指定會話。用快捷鍵
CTRL-ad
來暫時斷開當前會話。
[root@pvcent107 ~]# screen -dmS Urumchi [root@pvcent107 ~]# screen -list There is a screen on: 12842.Urumchi (Detached) 1 Socket in /tmp/screens/S-root. [root@pvcent107 ~]# screen -r Urumchi |
當我們用“-r”連接到screen會話後,我們就可以在這個僞終端裏面爲所欲爲,再也不用擔心HUP信號會對我們的進程造成影響,也不用給每個命令前都加上“nohup”或者“setsid”了。這是爲什麼呢?讓我來看一下下面兩個例子吧。
[root@pvcent107 ~]# ping www.google.com & [1] 9499 [root@pvcent107 ~]# pstree -H 9499 init─┬─Xvnc ├─acpid ├─atd ├─2*[sendmail] ├─sshd─┬─sshd───bash───pstree │ └─sshd───bash───ping |
我們可以看出,未使用screen時我們所處的bash是sshd的子進程,當ssh斷開連接時,HUP信號自然會影響到它下面的所有子進程(包括我們新建立的ping進程)。
[root@pvcent107 ~]# screen -r Urumchi [root@pvcent107 ~]# ping www.ibm.com & [1] 9488 [root@pvcent107 ~]# pstree -H 9488 init─┬─Xvnc ├─acpid ├─atd ├─screen───bash───ping ├─2*[sendmail] |
而使用了screen後就不同了,此時bash是screen的子進程,而screen是init(PID爲1)的子進程。那麼當ssh斷開連接時,HUP信號自然不會影響到screen下面的子進程了。
現在幾種方法已經介紹完畢,我們可以根據不同的場景來選擇不同的方案。nohup/setsid無疑是臨時需要時最方便的方法,disown能幫助我們來事後補救當前已經在運行了的作業,而screen則是在大批量操作時不二的選擇了。
“系統管理員工具包:進程管理技巧”(developerWorks中國,2006年5月)介紹了Linux進程管理的更多技巧。
“Linux技巧:使用screen管理你的遠程會話”(developerWorks中國,2007年7月)介紹了screen的更多技巧。
在developerWorks中國網站Linux專區中學習更多Linux方面的知識。
原文地址:https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
轉載請註明原文出處及原作者。