咱們常常會碰到這樣的問題,用 telnet/ssh 登陸了遠程的 Linux 服務器,運行了一些耗時較長的任務, 結果卻因爲網絡的不穩定致使任務中途失敗。如何讓命令提交後不受本地關閉終端窗口/網絡斷開鏈接的干擾呢?下面舉了一些例子, 您能夠針對不一樣的場景選擇不一樣的方式來處理這個問題。linux
前置知識:shell
xterm,console,tty,pts,pty的區別windows
· shell是直接和內核進行通訊的東西 bash
· xterm是一個軟件概念,能夠經過這個程序鏈接到console從而控制主機,能夠理解爲cli形式的終端模擬器,而gnome-terminal,konsole就是gui形式的終端模擬器服務器
· console是主機的控制檯,是一個物理概念。 網絡
· tty、pty、pts都是終端,是硬件或者設備概念。 session
· tty是全部終端設備的總稱 app
· pty是其中一類,僞終端,或者叫虛擬終端 less
nohup/setsid/&ssh
場景1:
若是隻是臨時有一個命令須要長時間運行,什麼方法能最簡便的保證它在後臺穩定運行呢?
解決方法:
咱們知道,當用戶註銷(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 文件中。通常咱們可在結尾加上"&"來將命令同時放入後臺運行,也可用">filename 2>&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 ~]# |
nohup描述:Run COMMAND, ignoring hangup signals.(忽略任何中斷/掛起信號,使命令繼續執行)
可是當你嘗試使用命令:
1 |
nohup command |
時候卻會遇到不大不小的麻煩……
delectate@delectate:~$ nohup vlc
nohup: ignoring input and appending output to `nohup.out'
是的,雖然它自動把debug信息記錄到nohup.out文件,可是你卻沒法使用這個終端進行任何操做。
因此你須要和第一個方法混用,即
nohupcommand {option}&
混用後,它會自動把你執行的命令輸出結果記錄到權限爲-rw——-,名爲nohup.out的文件中。
可是你仍然須要
delectate@delectate:~$ nohup vlc &
[1] 9045
delectate@delectate:~$ nohup: ignoringinput and appending output to `nohup.out'
//在這裏按一下回車或以ctrl+c以
//show a clean terminal
delectate@delectate:~$
與使用 「&」性質相同,當前啓動程序的終端若是沒有被關閉,已經啓動的程序附在pst上;若是終端被關閉,則自動附在tty。
若是當前目錄的 nohup.out 文件不可寫,輸出重定向到 $HOME/nohup.out。默認狀態下,nohup默認輸出到nohup.out文件,你也能夠利用重定向來指定輸出文件:
nohupcommand {option} > myout.file 2>&1 &
只有當虛擬終端是 $ 或者 # 時候,才能夠關閉此終端,不然可能致使已經啓動的進程被關閉(按enter——若是程序持續輸出信息而沒有出現 $ 或 #)
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 信號的影響了。
回頁首
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. |
能夠看出,咱們能夠用以下方式來達成咱們的目的。
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
來將它放入後臺並繼續運行。須要注意的是,若是掛起會影響當前進程的運行結果,請慎用此方法。
[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 |
[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]# |
回頁首
screen
場景:
咱們已經知道了如何讓進程免受 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 -dmS session name
來創建一個處於斷開模式下的會話(並指定其會話名)。screen -list
來列出全部會話。screen -r session name
來從新鏈接指定會話。CTRL-a d
來暫時斷開當前會話。 [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 則是在大批量操做時不二的選擇了。