2011-01-21 76 views
3

寫守護我有一個由跑了.py文件:蟒蛇:如何在Linux的

蟒蛇a.py &

我使用的是ssh來運行命令,之後我有註銷。過了一段時間後,我發現退出了這個過程。我懷疑這是Linux向它發送一些信號?我想如果我可以製作守護進程,那麼我可以避免這種情況?

回答

7

雖然nohup的將工作,這是一個快速和骯髒的解決方案。要創建一個適當的守護進程,你需要使用SysV init或者(如果你運行的是Ubuntu 6.10+或者Fedora 9+)暴發戶。

這裏有一個簡單的腳本開始a.py並重新啓動它時,它就會被殺死(5次5分鐘內跨度):

respawn 

respawn limit 5 300 

exec python /path/to/a.py 

然後,只需把該腳本/etc/init/

Upstart還有很多選擇。檢查Quick Start教程。

3

與「nohup的」運行它忽略的信號時,你的shell退出:

nohup python a.py & 
1

您也可以使用屏幕實用程序,允許您在單個終端窗口或遠程終端會話內訪問多個單獨的終端會話。

這意味着您可以設置一個屏幕會話(使用您選擇的名稱),在其中啓動一個程序(例如使用&),從會話中分離並稍後重新連接。

啓動未命名的屏幕。

$ screen 

要具有特定名稱使用創建一個新的會話:

$ screen -S backup 

- 這些都命令創建一個新的持久的會話,你可以使用它作爲一個普通的終端窗口,即發出指令並運行腳本。

如果你想離開會話而不終止它,使用:

Ctrl+a d command (press and hold Ctrl, press and hold a, then press d) to detach from the session. 

要查看runnging屏幕的列表:

$ screen -ls 

到正在運行的屏幕連接到控制檯:

$ screen -R 

當屏幕正在運行時,可以使用以下鍵組合,並將其連接到控制檯。所有關鍵組合開始於按壓控制和同時開始。

ctrl+a d - detach the screen, and let it run without user interface (as described above) 
ctrl+a c - create a new terminal 
ctrl+a A - set the name of the current terminal 
ctrl+a n - switch to next terminal 
ctrl+a p - switch to prev terminal 
ctrl+a " - list the of terminals