如何在 unix 中守護任意腳本? - How do I daemonize an arbitrary script in unix?

問題:

I'd like a daemonizer that can turn an arbitrary, generic script or command into a daemon .我想要一個可以將任意的通用腳本或命令轉換爲daemon 的守護程序

There are two common cases I'd like to deal with:我想處理兩種常見情況:

  1. I have a script that should run forever.我有一個應該永遠運行的腳本。 If it ever dies (or on reboot), restart it.如果它死了(或在重新啓動時),請重新啓動它。 Don't let there ever be two copies running at once (detect if a copy is already running and don't launch it in that case).不要讓兩個副本同時運行(檢測一個副本是否已經在運行,在這種情況下不要啓動它)。

  2. I have a simple script or command line command that I'd like to keep executing repeatedly forever (with a short pause between runs).我有一個簡單的腳本或命令行命令,我想永遠重複執行(在運行之間有短暫的停頓)。 Again, don't allow two copies of the script to ever be running at once.同樣,不要讓腳本的兩個副本同時運行。

Of course it's trivial to write a "while(true)" loop around the script in case 2 and then apply a solution for case 1, but a more general solution will just solve case 2 directly since that applies to the script in case 1 as well (you may just want a shorter or no pause if the script is not intended to ever die (of course if the script really does never die then the pause doesn't actually matter)).當然,在案例 2 中圍繞腳本編寫一個“while(true)”循環,然後爲案例 1 應用解決方案是微不足道的,但更通用的解決方案將直接解決案例 2,因爲這適用於案例 1 中的腳本好(你可能只是想更短或沒有停頓如果腳本不打算會死(當然如果腳本確實從來沒有再死暫停實際上並沒有物質))。

Note that the solution should not involve, say, adding file-locking code or PID recording to the existing scripts.請注意,該解決方案不應涉及,例如,向現有腳本添加文件鎖定代碼或 PID 記錄。

More specifically, I'd like a program "daemonize" that I can run like更具體地說,我想要一個程序“守護進程”,我可以像

% daemonize myscript arg1 arg2

or, for example,或者,例如,

% daemonize 'echo `date` >> /tmp/times.txt'

which would keep a growing list of dates appended to times.txt.這將保留一個不斷增長的日期列表附加到 times.txt。 (Note that if the argument(s) to daemonize is a script that runs forever as in case 1 above, then daemonize will still do the right thing, restarting it when necessary.) I could then put a command like above in my .login and/or cron it hourly or minutely (depending on how worried I was about it dying unexpectedly). (請注意,如果 daemonize 的參數是一個永遠運行的腳本,如上面的情況 1,那麼 daemonize 仍然會做正確的事情,在必要時重新啓動它。)然後我可以在我的 .login 中放置一個類似上面的命令和/或每小時或每分鐘一次(取決於我對它意外死亡的擔心程度)。

NB: The daemonize script will need to remember the command string it is daemonizing so that if the same command string is daemonized again it does not launch a second copy.注意:daemonize 腳本需要記住它正在守護的命令字符串,以便如果再次守護相同的命令字符串,它不會啓動第二個副本。

Also, the solution should ideally work on both OS X and linux but solutions for one or the other are welcome.此外,理想情況下,該解決方案應該適用於 OS X 和 Linux,但歡迎使用其中之一的解決方案。

EDIT: It's fine if you have to invoke it with sudo daemonize myscript myargs .編輯:如果您必須使用sudo daemonize myscript myargs調用它, sudo daemonize myscript myargs

(If I'm thinking of this all wrong or there are quick-and-dirty partial solutions, I'd love to hear that too.) (如果我認爲這一切都錯了,或者有快速而骯髒的部分解決方案,我也很想聽聽。)


PS: In case it's useful, here's a similar question specific to python. PS:如果它有用, 這裏有一個特定於python的類似問題。

And this answer to a similar question has what appears to be a useful idiom for a quick-and-dirty demonizing of an arbitrary script: 這個對類似問題的回答似乎是一個有用的成語,用於快速和骯髒地妖魔化任意腳本:


解決方案:

參考: https://stackoom.com/en/question/2Cdj
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章