9 10 nohup 使用者退出系統程序繼續工作

2022-08-11 10:12:09 字數 2772 閱讀 2689

可以將程式以忽略掛起訊號的方式執行起來,被執行程式的輸出資訊將不會顯示到終端。

無論是否將nohup命令的輸出重定向到終端,輸出都將寫入到當前目錄的nohup.out檔案中。如果當前目錄的nohup.out檔案不可寫,則輸出重定向到$home/nohup.out檔案中。

正常情況下,如果使用者退出登入或會話終止,則使用者正在執行並可持續一段時間的命令(非守護程序)將自動終止。使用nohup命令可以實現在使用者退出或當前會話終止後繼續保持執行,具體的例子如下:

[root@cs6 ~]# nohup ping

g.cn

nohup: 忽略輸入並把輸出追加到

"nohup.out"#

<==當前終端已經hang住,此時強制關閉當前終端(例如關閉該標籤或者ss日客戶端工具),這個ping命令依然會在後台執行。

# 但是ctrl +c 會退出命令執行

[root@cs6 ~]# ps -ef|grep

ping

root

181810

19:19 ? 00:00:00

ping

g.cn

root

1820

1797

019:19 pts/1

00:00:00

grep

ping

[root@cs6 ~]# tail -f nohup.out

64 bytes from 203.208.40.79: icmp_seq=97 ttl=128

time=42.2

ms64 bytes from 203.208.40.79: icmp_seq=98 ttl=128

time=42.7

ms64 bytes from 203.208.40.79: icmp_seq=99 ttl=128

time=42.7

ms64 bytes from 203.208.40.79: icmp_seq=100 ttl=128

time=42.7

ms64 bytes from 203.208.40.79: icmp_seq=101 ttl=128

time=42.6

ms64 bytes from 203.208.40.79: icmp_seq=102 ttl=128

time=42.4

ms64 bytes from 203.208.40.79: icmp_seq=103 ttl=128

time=43.0

ms64 bytes from 203.208.40.79: icmp_seq=104 ttl=128

time=50.7

ms64 bytes from 203.208.40.79: icmp_seq=105 ttl=128

time=42.9

ms64 bytes from 203.208.40.79: icmp_seq=106 ttl=128

time=43.0 ms

在工作中我們一般會配合&符執行nohup命令,讓程式直接在後台執行:

[root@cs6 ~]# nohup ping g.cn &[

1] 1855

[root@cs6 ~]# nohup: 忽略輸入並把輸出追加到"

nohup.out

"[root@cs6 ~]# tail -f nohup.out

64 bytes from 203.208.40.95: icmp_seq=8 ttl=128

time=42.1

ms64 bytes from 203.208.40.95: icmp_seq=9 ttl=128

time=43.3

ms64 bytes from 203.208.40.95: icmp_seq=10 ttl=128

time=44.1

ms64 bytes from 203.208.40.95: icmp_seq=11 ttl=128

time=42.5

ms64 bytes from 203.208.40.95: icmp_seq=12 ttl=128

time=42.9

ms64 bytes from 203.208.40.95: icmp_seq=13 ttl=128

time=42.6

ms64 bytes from 203.208.40.95: icmp_seq=14 ttl=128

time=42.8

ms64 bytes from 203.208.40.95: icmp_seq=15 ttl=128

time=42.8

ms64 bytes from 203.208.40.95: icmp_seq=16 ttl=128

time=134

ms64 bytes from 203.208.40.95: icmp_seq=17 ttl=128

time=42.8

ms64 bytes from 203.208.40.95: icmp_seq=18 ttl=128

time=42.2

ms64 bytes from 203.208.40.95: icmp_seq=19 ttl=128

time=43.2

ms64 bytes from 203.208.40.95: icmp_seq=20 ttl=128

time=43.7

ms64 bytes from 203.208.40.95: icmp_seq=21 ttl=128

time=42.0

ms

類似功能的命令還有screen和直接使用&符。

用screen 取代nohup命令

大家在起後台服務,或者長時間執行某個指令碼的時候。是不是感覺,互動性很差,有時,想把結果直接在終端上輸出,又怕不小心關閉了終端導致程序退出?有些sshd服務還會設定連線超時,自動退出終端 現在用screen這個指令可以很好的解決跑後台服務的尷尬。命令其實超簡單的 直接在終端上輸入 screen 這個...

Ubuntu9 10 用SSH實現伺服器

1 安裝ssh server sudo apt get install openssh server linux中的網路服務在資料夾 etc init.d 中都能找到,以ssh服務為例 要啟動某個服務,可用如下格式 sudo etc init.d ssh start 停止服務 sudo etc in...

用Nohup命令讓Linux下的程式在後台執行

unix linux下一般想讓某個程式在後台執行,很多都是使用 在程式結尾來讓程式自動執行。比如我們要執行mysql在後台 usr local mysql bin mysqld safe user mysql 但是我們很多程式並不象mysqld一樣可以做成守護程序,可能我們的程式只是普通程式而已,一...