2014-09-22 61 views
1

我在測試用nohup調用的命令是否確實沒有得到SIGHUP信號。儘管被nohup調用,進程仍能繼續運行,即使它收到SIGHUP?

我確認如果使用PuTTY登錄我的CentOS盒子,請運行nohup python foo.py &其中foo.py包含一個無限循環,如果我終止PuTTY,python程序仍會繼續運行。

然後我寫了sig.py,它處理並記錄它接收到sig.txt的所有信號。

import signal, os 

def handler(sig, frame): 
    f = open('sig.txt', 'a') 
    f.write('signal: ' + str(sig) + '\n') 
    f.close() 

for i in range(1, 20): 
    if i in [9, 19]: 
     continue 
    print 'Setting signal handler for', i 
    signal.signal(i, handler) 

f = open('sig.txt', 'w') 
f.write('start\n') 
f.close() 

while True: 
    pass 

然後我在PuTTY終端中運行nohup python sig.py &。然後關閉PuTTY終端。

我打開一個新的PuTTY終端並檢查sig.txt的內容,並在其中找到它。

$ cat sig.txt 
start 
signal: 1 

所以看來運行的進程仍然收到SIGHUP。

如果正在運行的進程仍然收到SIGHUP,它如何在SIGHUP中調用時保持活躍狀態​​?

回答

0

根據維基百科nohupignores SIGHUP而disown實際上是prevents的信號被髮送。在你的python腳本中,你可以設置所有信號的信號處理程序,從而覆蓋nohup的功能。

相關問題