咱們常常會碰到這樣的問題,用 telnet/ssh 登陸了遠程的 Linux 服務器,運行了一些耗時較長的任務, 結果卻因爲網絡的不穩定致使任務中途失敗。如何讓命令提交後不受本地關閉終端窗口/網絡斷開鏈接的干擾呢?下面舉了一些例子, 您能夠針對不一樣的場景選擇不一樣的方式來處理這個問題。 nohup/setsid/& 場景: 若是隻是臨時有一個命令須要長時間運行,什麼方法能最簡便的保證它在後臺穩定運行呢? hangup 名稱的來由 在 Unix 的早期版本中,每一個終端都會經過 modem 和系統通信。當用戶 logout 時,modem 就會掛斷(hang up)電話。 同理,當 modem 斷開鏈接時,就會給終端發送 hangup 信號來通知其關閉全部子進程。 解決方法: 咱們知道,當用戶註銷(logout)或者網絡斷開時,終端會收到 HUP(hangup)信號從而關閉其全部子進程。所以,咱們的解決辦法就有兩種途徑:要麼讓進程忽略 HUP 信號,要麼讓進程運行在新的會話裏從而成爲不屬於此終端的子進程。shell
NAME nohup - run a command immune to hangups, with output to a non-ttywindows
SYNOPSIS nohup COMMAND [ARG]... nohup OPTIONbash
DESCRIPTION Run COMMAND, ignoring hangup signals.服務器
--help display this help and exit --version output version information and exit
可見,nohup 的使用是十分方便的,只需在要處理的命令前加上 nohup 便可,標準輸出和標準錯誤缺省會被重定向到 nohup.out 文件中。通常咱們可在結尾加上"&"來將命令同時放入後臺運行,也可用">filename 2>&1"來更改缺省的重定向文件名。 nohup 示例 [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 sessionsession
SYNOPSIS setsid program [ arg ... ]app
DESCRIPTION setsid runs a program in a new session. 可見 setsid 的使用也是很是方便的,也只需在要處理的命令前加上 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 信號的影響吧。 subshell 示例 [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 信號的影響了。 回頁首 disown 場景: 咱們已經知道,若是事先在命令前加上 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. 能夠看出,咱們能夠用以下方式來達成咱們的目的。 靈活運用 CTRL-z 在咱們的平常工做中,咱們能夠用 CTRL-z 來將當前進程掛起到後臺暫停運行,執行一些別的操做,而後再用 fg 來將掛起的進程從新放回前臺(也可用 bg 來將掛起的進程放在後臺)繼續運行。這樣咱們就能夠在一個終端內靈活切換運行多個任務,這一點在調試代碼時尤其有用。由於將代碼編輯器掛起到後臺再從新放回時,光標定位仍然停留在上次掛起時的位置,避免了從新定位的麻煩。 用disown -h jobspec來使某個做業忽略HUP信號。 用disown -ah 來使全部的做業都忽略HUP信號。 用disown -rh 來使正在運行的做業忽略HUP信號。 須要注意的是,當使用過 disown 以後,會將把目標做業從做業列表中移除,咱們將不能再使用jobs來查看它,可是依然可以用ps -ef查找到它。 可是還有一個問題,這種方法的操做對象是做業,若是咱們在運行命令時在結尾加了"&"來使它成爲一個做業並在後臺運行,那麼就萬事大吉了,咱們能夠經過jobs命令來獲得全部做業的列表。可是若是並無把當前命令做爲做業來運行,如何才能獲得它的做業號呢?答案就是用 CTRL-z(按住Ctrl鍵的同時按住z鍵)了! CTRL-z 的用途就是將當前進程掛起(Suspend),而後咱們就能夠用jobs命令來查詢它的做業號,再用bg jobspec來將它放入後臺並繼續運行。須要注意的是,若是掛起會影響當前進程的運行結果,請慎用此方法。 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 largeFile2less
[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]# 回頁首 screen 場景: 咱們已經知道了如何讓進程免受 HUP 信號的影響,可是若是有大量這種命令須要在穩定的後臺裏運行,如何避免對每條命令都作這樣的操做呢? 解決方法: 此時最方便的方法就是 screen 了。簡單的說,screen 提供了 ANSI/VT100 的終端模擬器,使它可以在一個真實終端下運行多個全屏的僞終端。screen 的參數不少,具備很強大的功能,咱們在此僅介紹其經常使用功能以及簡要分析一下爲何使用 screen 可以避免 HUP 信號的影響。咱們先看一下 screen 的幫助信息: SCREEN(1) SCREEN(1)ssh
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 -dmS session name來創建一個處於斷開模式下的會話(並指定其會話名)。 用screen -list 來列出全部會話。 用screen -r session name來從新鏈接指定會話。 用快捷鍵CTRL-a d 來暫時斷開當前會話。 screen 示例 [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」了。這是爲何呢?讓我來看一下下面兩個例子吧。