我知道在启动守护进程文件夹中使用plist时,有一个名为<StartInterval>的键。我更喜欢在可能的时候使用launchctl,因为它比写一个完整的plist要快得多,但是我还没有弄清楚如何在进程被杀死后重新启动它。我已经看过手册了,在那里什么也没有发现。有办法吗?通常,我使用以下命令:
launchctl submit -l somename -p /path/to/script -o output.txt -e errors.txt
但是,如果程序在任何时间间隔后被终止,这将不会重新启动程序。
发布于 2014-01-15 23:34:11
如果程序因某种原因终止,launchctl submit应该已经再次运行了:
submit -l label [-p executable] [-o path] [-e path] -- command [args]
A simple way of submitting a program to run without a configura-
tion file. This mechanism also tells launchd to keep the program
alive in the event of failure.
-l label
What unique label to assign this job to launchd.
-p program
What program to really execute, regardless of what fol-
lows the -- in the submit sub-command.
-o path Where to send the stdout of the program.
-e path Where to send the stderr of the program.若要再次运行程序(如果程序退出时有错误),请将用于SuccessfulExit的KeepAlive条件设置为false:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>test</string>
<key>ProgramArguments</key>
<array>
<string>bash</string>
<string>-c</string>
<string>n=$((RANDOM%2));say $n;exit $n</string>
</array>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>启动节流作业,所以程序需要10秒左右才能恢复。
https://stackoverflow.com/questions/21062972
复制相似问题