天天看點

nohup指令和&使用

作者:寒笛過霜天

nohup

nohup 指令運作由 Command參數和任何相關的 Arg參數指定的指令, 忽略所有挂斷(SIGHUP) 信号。

在登出後使用 nohup 指令運作背景中的程式。要運作背景中的 nohup 指令, 添加 & ( 表示“and”的符号) 到指令的尾部。

nohup 是 no hang up 的縮寫, 就是不挂斷的意思。

nohup指令:如果你正在運作一個程序, 而且你覺得在退出帳戶時該程序還不會結束, 那麼可以使用nohup指令。該指令可以在你退出帳戶/關閉終端之後繼續運作相應的程序。

在預設情況下該作業的所有輸出都被重定向到一個名為nohup.out的檔案中。

nohup和&的差別

& : 指在背景運作

nohup : 不挂斷的運作, 注意并沒有背景運作的功能, 就是指, 用nohup運作指令可以使指令永久的執行下去, 和使用者終端沒有關系,

例如我們斷開SSH連接配接都不會影響他的運作, 注意了nohup沒有背景運作的意思;&才是背景運作

linux重定向:

0、1和2分别表示标準輸入、标準輸出和标準錯誤資訊輸出, 可以用來指定需要重定向的标準輸入或輸出。

Linux下還有一個特殊的檔案/dev/null, 它就像一個無底洞, 所有重定向到它的資訊都會消失得無影無蹤。

這一點非常有用, 當我們不需要回顯程式的所有資訊時, 就可以将輸出重定向到/dev/null。

如果想要錯誤資訊都不顯示, 則要标準錯誤都重定向到/dev/null, 例如:

ls l>/dev/null

如果想要正常輸出和錯誤資訊都不顯示, 則要把标準輸出和标準錯誤都重定向到/dev/null, 例如:

# ls 1>/dev/null 2>/dev/null

還有一種做法是将錯誤重定向到标準輸出, 然後再重定向到 /dev/null, 例如:

# ls >/dev/null 2>&1

注意:此處的順序不能更改, 否則達不到想要的效果, 此時先将标準輸出重定向到 /dev/null, 然後将标準錯誤重定向到标準輸,

由于标準輸出已經重定向到了/dev/null, 是以标準錯誤也會重定向到/dev/null, 于是一切靜悄悄。

2>&1: 了解是将标準錯誤(2)重定向到标準輸出(1)

注意: ls 1>/dev/null ">" 後面不能有空格

在使用nohup指令的時候, 經常由于輸出nohup.out的路徑沒有寫入權限, 而無法使用nohup。

nohup: ignoring input and appending output to 'nohup.out'

這是可以使用Linux重定向的方法, 将nohup.out重定向至一個有寫入權限的路徑, 或者直接扔到/dev/null中。

解決方法:

nohup php ws_server.php >/dev/null 2>/dev/null &

簡化:

nohup php ws_server.php >/dev/null 2>&1 &

輸入到日志檔案中

nohup ./webcron 2>&1 > error.log &

檢視運作的背景程序

> # jobs -l

[1]+ 54384 Running nohup php ws_server.php > /dev/null 2>&1 &

jobs指令隻看目前終端生效的, 關閉終端後, 在另一個終端jobs已經無法看到背景跑得程式了, 此時利用ps(程序檢視指令)

根據程序名查詢

> # ps -ef | grep php

root 6973 1 0 17:28 ? 00:00:02 php-fpm: master process (/www/server/php/56/etc/php-fpm.conf)

www 6983 6973 0 17:28 ? 00:00:00 php-fpm: pool www

www 6984 6973 0 17:28 ? 00:00:00 php-fpm: pool www

www 6985 6973 0 17:28 ? 00:00:00 php-fpm: pool www

www 6986 6973 0 17:28 ? 00:00:00 php-fpm: pool www

www 6987 6973 0 17:28 ? 00:00:00 php-fpm: pool www

root 6999 1 0 17:28 ? 00:00:02 php-fpm: master process (/www/server/php/73/etc/php-fpm.conf)

www 7007 6999 0 17:28 ? 00:00:00 php-fpm: pool www

www 7008 6999 0 17:28 ? 00:00:00 php-fpm: pool www

www 7009 6999 0 17:28 ? 00:00:00 php-fpm: pool www

www 7010 6999 0 17:28 ? 00:00:00 php-fpm: pool www

www 7011 6999 0 17:28 ? 00:00:00 php-fpm: pool www

root 54384 8612 0 21:19 pts/0 00:00:00 php ws_server.php

root 54385 54384 0 21:19 pts/0 00:00:00 php ws_server.php

root 54388 54385 0 21:19 pts/0 00:00:00 php ws_server.php

root 54389 54385 0 21:19 pts/0 00:00:00 php ws_server.php

root 57989 8612 0 21:23 pts/0 00:00:00 grep --color=auto php

> # kill -9 54384

根據端口号查詢程序ID号(PHP代碼中開啟websocker服務的端口号)

> # netstat -lnp | grep 9502

tcp 0 0 0.0.0.0:9502 0.0.0.0:* LISTEN 54384/php

> # kill -9 54384

執行個體: wget指令下載下傳檔案不中斷

nohup wget https://gitlab.com/humingzhe/Linux/-/archive/master/Linux-master.zip

執行個體: 監聽 8811 端口服務是否存在(傳回值1 和 0)

netstat -anp 2>/dev/null | grep 8811 | grep LISTEN | wc -l

bg 和 fg 使用

> # nohup wget https://gitlab.com/humingzhe/Linux/-/archive/master/Linux-master.zip &

檢視運作的背景程序

> # jobs -l

[1]+ 24729 運作中 nohup wget https://gitlab.com/humingzhe/Linux/-/archive/master/Linux-master.zip &

背景轉前台

> # fg %1

nohup wget https://gitlab.com/humingzhe/Linux/-/archive/master/Linux-master.zip

使用 Ctrl+Z 快捷鍵的方式, 可以将前台工作放入背景, 但是會處于暫停狀态, 然後恢複運作

> # bg %1

[1]+ nohup wget https://gitlab.com/humingzhe/Linux/-/archive/master/Linux-master.zip &

fg 指令用于把背景工作恢複到前台執行,該指令的基本格式如下:

#fg %工作号

使用 Ctrl+Z 快捷鍵的方式, 可以将前台工作放入背景, 但是會處于暫停狀态; 那麼, 有沒有辦法可以讓背景工作繼續在背景執行呢? 答案是肯定的, 這就需要用到 bg 指令。

bg 指令的基本格式如下:

> # bg %工作号

以上bg、fg、Ctrl+Z 快捷鍵 均可以交替進行;

檢視目前檔案下載下傳的大小

> # du -h Linux-master.zip

64M Linux-master.zip

動态檢視檔案下載下傳的記錄

> # tail -f nohup.out

48000K .......... .......... .......... .......... .......... 172M

48050K .......... .......... .......... .......... .......... 150M

48100K .......... .......... .......... .......... .......... 212M

48150K .......... .......... .......... .......... .......... 741K

注意: 超大檔案推薦使用 aria2 下載下傳工具, 見 Linux | 技巧 | 多線程下載下傳超大檔案--aria2

結論: nohup和 & 使用

使用&背景運作程式:

結果會輸出到終端

使用Ctrl + C發送SIGINT信号, 程式免疫

關閉session(終端)發送SIGHUP信号, 程式關閉

使用nohup運作程式:

結果預設會輸出到nohup.out

使用Ctrl + C發送SIGINT信号, 程式關閉

關閉session發送SIGHUP信号, 程式免疫

日後使用

平日線上經常使用nohup和&配合來啟動程式nohup ./test &

同時免疫SIGINT和SIGHUP信号

繼續閱讀