Linux下進程放入後臺執行幾種方法:
1. nohup
顧名思義,nohup的用途就是讓提交的命令忽略所有的hangup信號。
使用方法:nohup COMMAND [ARG]…
1 2 3 4 5 6 7 | [root@pvcent107 ~]# nohup ping www.ibm.com & [1] 3059 nohup: appending output to `nohup.out' [root@pvcent107 ~]# ps -ef |grep 3059 root 3059 <strong>984</strong> 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
在一個新的會話中運行命令,從而可以避開當前終端發出的HUP信號。
使用方法:setsid COMMAND [ARG]…
1 2 3 4 5 | [root@pvcent107 ~]# setsid ping www.ibm.com [root@pvcent107 ~]# ps -ef |grep www.ibm.com root 31094 <strong>1</strong> 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 ~]# |
3. &
可以結合()產生一個新的子shell並在這個子shell中將任務放置到後臺運行,從而不受當前shell終端的HUP信號影響。
使用方法:(COMMAND [ARG]… &)
1 2 3 4 5 | [root@pvcent107 ~]# (ping www.ibm.com &) [root@pvcent107 ~]# ps -ef |grep www.ibm.com root 16270 <strong>1</strong> 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 ~]# |
而我通常的使用方式爲:
nohup ./filename.sh > filename.log 2>&1 &
三點理由:
1)nohup保障進程不會被hangup信號異常中斷;
2)將任務放置到後臺運行,不佔用當前的終端;
3)將錯誤輸出也打印到log中,默認>只有標準輸出,錯誤輸出沒有。
4.控制進程
通過以下命令,我們可以對放入到後臺的命令進行控制
查看當前終端下的後臺進程:
直接執行:jobs
將查看到的某個後臺進程放回到前臺:
直接輸入:fg {jobid} //這裏的{jobid}是通過jobs命令中看到的進程前[]中的數字。
將當前正在前臺運行的進程放到後臺運行:
先敲下快捷鍵:ctrl +z //暫停當前正在運行的進程。
再執行:bg
終止當前正在前臺運行的進程:
直接敲下快捷鍵:ctrl +c
查看當前後臺進程 jobs -l
5.disown
亡羊補牢,爲沒有使用nohup與setsid的進程加上忽略HUP信號的功能。
使用方法:
將當前正在前臺運行的進程放到後臺運行;
然後執行disown -h %{jobid} //這裏的{jobid}是通過jobs命令中看到的進程前[]中的數字。
- 用
disown -h jobspec
來使某個作業忽略HUP信號。 - 用
disown -ah
來使所有的作業都忽略HUP信號。 - 用
disown -rh
來使正在運行的作業忽略HUP信號。 - disown 示例1(如果提交命令時已經用“&”將命令放入後臺運行,則可以直接使用“disown”)123456789[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
6.通過screen來實現穩定的後臺運行
screen是建立一個新的全屏虛擬會話終端,這個會話只有在手動輸入exit的時候纔會退出,在這個會話裏執行的命令不用擔心HUP信號會對我們的進程 造成影響,因此也不用給每個命令前都加上“nohup”或“setsid”了,非常適合我們有規劃的執行大量的後臺任務,可以非常方便的讓我們對這些後臺 任務進行管理。
使用方法:
screen //立即創建並進入一個會話。
screen -dmS {name} //建立一個處於斷開模式下的會話,並根據我們的需要指定其會話名稱。
screen -list //列出所有會話。
screen -r {name} //進入指定會話。
ctrl +ad //輸入快捷鍵ctrl +a和d,可暫時退出當前會話。
exit //進入指定會話後執行exit即可關閉該會話。
1 2 3 4 5 6 7 8 9 | [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 <a name="screen1"></a><strong>1. 未使用 screen 時新進程的進程樹</strong> |
|
1 |
我們可以看出,未使用 screen 時我們所處的 bash 是 sshd 的子進程,當 ssh 斷開連接時,HUP 信號自然會影響到它下面的所有子進程(包括我們新建立的 ping 進程)。
1 | <a name="screen2"></a><strong>2. 使用了 screen 後新進程的進程樹</strong> |
1 2 3 4 5 6 7 8 9 | [root@pvcent107 ~]# screen -r Urumchi [root@pvcent107 ~]# ping www.ibm.com & [1] 9488 [root@pvcent107 ~]# pstree -H 9488 init─┬─Xvnc ├─acpid ├─atd <strong>├─screen───bash───ping</strong> ├─2*[sendmail] |