linux中如何讓進程在後臺運行

Linux技巧linux

一. nohup / setsid / &

使用場景:
若是隻是臨時有一個命令須要長時間運行,什麼方法能最簡便的保證它在後臺穩定運行呢?shell

咱們的解決辦法就有兩種途徑:要麼讓進程忽略 HUP 信號,要麼讓進程運行在新的會話裏從而成爲不屬於此終端的子進程。bash

解決方法:session

1.nohup

只需在要處理的命令前加上 nohup 便可,標準輸出和標準錯誤缺省會被重定向到 nohup.out 文件中。通常咱們可在結尾加上"&"來將命令同時放入後臺運行,也可用>filename 2>&1來更改缺省的重定向文件名。app

[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

setsid 的使用也是很是方便的,也只需在要處理的命令前加上 setsid 便可。ssh

[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 作比較。ui

3. &

將一個或多個命名包含在「()」中就能讓這些命令在子 shell 中運行中
當咱們將"&"也放入「()」內以後,咱們就會發現所提交的做業並不在做業列表中,也就是說,是沒法經過jobs來查看的。google

[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 信號的影響了。code

二. disown

使用場景:若是事先在命令前加上 nohup 或者 setsid 就能夠避免 HUP 信號的影響。可是若是咱們未加任何處理就已經提交了命令,該如何補救才能讓它避免 HUP 信號的影響呢?進程

解決方法:這時想加 nohup 或者 setsid 已經爲時已晚,只能經過做業調度和 disown 來解決這個問題了

  • 用disown -h jobspec來使某個做業忽略HUP信號。

  • 用disown -ah 來使全部的做業都忽略HUP信號。

  • 用disown -rh 來使正在運行的做業忽略HUP信號。

當使用過 disown 以後,會將把目標做業從做業列表中移除,咱們將不能再使用jobs來查看它,可是依然可以用ps -ef查找到它。

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]#

三: screen

使用場景:
咱們已經知道了如何讓進程免受 HUP 信號的影響,可是若是有大量這種命令須要在穩定的後臺裏運行,如何避免對每條命令都作這樣的操做呢?

解決方案:
此時最方便的方法就是 screen 了。簡單的說,screen 提供了 ANSI/VT100 的終端模擬器,使它可以在一個真實終端下運行多個全屏的僞終端。screen 的參數不少,具備很強大的功能,

  • 用screen -dmS (sessionName)來創建一個處於斷開模式下的會話(並指定其會話名)。

  • 用screen -list 來列出全部會話。

  • 用screen -r (sessionName)來從新鏈接指定會話。

  • 用快捷鍵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」了。

  1. 未使用 screen 時新進程的進程樹

[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 進程)。

  1. 使用了 screen 後新進程的進程樹

[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 下面的子進程了。

相關文章
相關標籤/搜索