當使用者登出/登入時,後台進程(nohup &)暫停/恢復

當使用者登出/登入時,後台進程(nohup &)暫停/恢復

Ubuntu伺服器11.10

當普通(非 root)使用者使用 nohup (nohup java ... &) 啟動進程時,當使用者從終端機 (ssh) 斷開連接時,進程將停止回應。當用戶再次連線時,該進程再次開始回應。例如,該進程是一個資料庫(H2),當啟動該進程的使用者與終端斷開連接時,它會停止回應查詢,並在使用者再次登入時再次開始回應。

是否有任何我不知道的後台進程權限阻止該進程在後台運行?什麼可能導致這種行為?

答案1

nohup應該對你有用,你的確切命令是什麼?也許你錯過了一些東西。這是維基百科上關於它的部分:

Nohupping backgrounded jobs is typically used to avoid terminating them when logging
off from a remote SSH session. A different issue that often arises in this situation
is that ssh is refusing to log off ("hangs"), since it refuses to lose any data
from/to the background job(s).  This problem can also be overcome by redirecting all
three I/O streams:

nohup ./myprogram > foo.out 2> foo.err < /dev/null &

所以你可以這樣做:

ssh -n -f user@remotebox "sh -c 'cd /foo/bar; nohup ./myprogram > foo.out 2> foo.err < /dev/null &'"

或者如果仍然不行,你可以嘗試screen。它將允許您在“後台”運行進程,並且在您登出後它將繼續運行。

首先,ssh轉到遠端框,然後從那裡使用並啟動您的進程,如果需要,screen您可以給您的會話名稱。screen您不會真正注意到任何不同,但在該會話中開始您的過程。您可以screen使用命令退出會話Ctrl-a d。它看起來像這樣:

user@remotebox:~$ screen -S foobarsession
user@remotebox:~$ startmyprocess
[detached from 4865.foobarsession]
user@remotebox:~$

然後您可以退出 ssh 會話,該進程將繼續運行。若要screen稍後重新連線到會話,請透過 ssh 返回遠端裝置並使用screen -r重新連線。您可以使用screen -ls來列出會話。

user@remotebox:~$ screen -ls
There is a screen on:
        4865.foobarsession     (10/05/2012 11:10:57 AM)     (Detached)
1 Socket in /var/run/screen/S-user
user@remotebox:~$ screen -r foobarsession
user@remotebox:~$ screen -ls
        4865.foobarsession     (10/05/2012 11:10:57 AM)     (Attached)
1 Socket in /var/run/screen/S-user
user@remotebox:~$

答案2

以 啟動的進程nohup ... &被設計為即使在終端進程退出後也能運作。我不認為有一個核心的unix機制可以掛起這樣的進程。儘管調度程序可能會更改其優先權(因為它可能被視為不太重要的進程)。

我很確定該進程會繼續運行,但是管道之類的東西會阻塞它並阻止回應。

相關內容