我知道在启动守护程序文件夹中使用plist时,会有一个名为<StartInterval>
的密钥。我更喜欢在可能时使用launchctl
,因为它比写出整个plist要快得多,但是我无法弄清楚如何让它在被杀死后重新启动。我已经阅读了手册页,但在那里找不到任何东西。有办法吗?通常我使用以下命令:
launchctl submit -l somename -p /path/to/script -o output.txt -e errors.txt
但如果程序在任何时间间隔后被杀死,则不会重新启动程序。
答案 0 :(得分:1)
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.
如果程序因错误退出而再次运行,请将KeepAlive的SuccessfulExit条件设置为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秒钟。