我們經常會碰到這樣的問題,用 telnet/ssh 登入了遠端的 Linux 伺服器,運作了一些耗時較長的任務, 結果卻由于網絡的不穩定導緻任務中途失敗。如何讓指令送出後不受本地關閉終端視窗/網絡斷開連接配接的幹擾呢?下面舉了一些例子, 您可以針對不同的場景選擇不同的方式來處理這個問題。
nohup/setsid/&
場景:
如果隻是臨時有一個指令需要長時間運作,什麼方法能最簡便的保證它在背景穩定運作呢?
解決方法:
我們知道,當使用者登出(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-ttySYNOPSIS nohup COMMAND [ARG]... nohup OPTIONDESCRIPTION Run COMMAND, ignoring hangup signals. --help display this help and exit --version output version information and exit |
可見,nohup 的使用是十分友善的,隻需在要處理的指令前加上 nohup 即可,标準輸出和标準錯誤預設會被重定向到 nohup.out 檔案中。一般我們可在結尾加上"&"來将指令同時放入背景運作,也可用
">filename2>&1"
來更改預設的重定向檔案名。
nohup 示例
[root@pvcent107 ~]# nohup ping www.ibm.com &[1] 3059nohup: appending output to `nohup.out'[root@pvcent107 ~]# ps -ef |grep 3059root 3059 984 0 21:06 pts/3 00:00:00 ping www.ibm.comroot 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 sessionSYNOPSIS setsid program [ arg ... ]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.comroot 31094 1 0 07:28 ? 00:00:00 ping www.ibm.comroot 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.comroot 16270 1 0 14:13 pts/4 00:00:00 ping www.ibm.comroot 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 ofactive jobs. If the -h option is given, each jobspec is notremoved from the table, but is marked so that SIGHUP is notsent to the job if the shell receives a SIGHUP. If no jobspecis present, and neither the -a nor the -r option is supplied,the current job is used. If no jobspec is supplied, the -aoption means to remove or mark all jobs; the -r option withouta jobspec argument restricts operation to running jobs. Thereturn value is 0 unless a jobspec does not specify a validjob. |
可以看出,我們可以用如下方式來達成我們的目的。
- 用
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 largeFileroot 4825 968 1 09:46 pts/4 00:00:00 cp -i -r testLargeFile largeFileroot 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 largeFile2root 5790 5577 1 10:04 pts/3 00:00:00 cp -i -r testLargeFile largeFile2root 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)NAME screen - screen manager with VT100/ANSI terminal emulationSYNOPSIS 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 -dmSsession name
來建立一個處于斷開模式下的會話(并指定其會話名)。 -
screen -list
來列出所有會話。 -
screen -rsession name
來重新連接配接指定會話。 - 用快捷鍵
CTRL-a d
來暫時斷開目前會話。
screen 示例
[root@pvcent107 ~]# screen -dmS Urumchi[root@pvcent107 ~]# screen -listThere is a screen on: 12842.Urumchi (Detached)1 Socket in /tmp/screens/S-root.[root@pvcent107 ~]# screen -r Urumchi |
當我們用“-r”連接配接到 screen 會話後,我們就可以在這個僞終端裡面為所欲為,再也不用擔心 HUP 信号會對我們的程序造成影響,也不用給每個指令前都加上“nohup”或者“setsid”了。這是為什麼呢?讓我來看一下下面兩個例子吧。
1. 未使用 screen 時新程序的程序樹
[root@pvcent107 ~]# ping www.google.com &[1] 9499[root@pvcent107 ~]# pstree -H 9499init─┬─Xvnc ├─acpid ├─atd ├─2*[sendmail] ├─sshd─┬─sshd───bash───pstree │ └─sshd───bash───ping |
我們可以看出,未使用 screen 時我們所處的 bash 是 sshd 的子程序,當 ssh 斷開連接配接時,HUP 信号自然會影響到它下面的所有子程序(包括我們建立立的 ping 程序)。
2. 使用了 screen 後新程序的程序樹
[root@pvcent107 ~]# screen -r Urumchi[root@pvcent107 ~]# ping www.ibm.com &[1] 9488[root@pvcent107 ~]# pstree -H 9488init─┬─Xvnc ├─acpid ├─atd ├─screen───bash───ping ├─2*[sendmail] |
而使用了 screen 後就不同了,此時 bash 是 screen 的子程序,而 screen 是 init(PID為1)的子程序。那麼當 ssh 斷開連接配接時,HUP 信号自然不會影響到 screen 下面的子程序了。