如何在 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章