2014-01-11 54 views
1

我知道在啓動守護進程文件夾中使用plist時,有一個叫做<StartInterval>的鍵。當我可以的時候,我更喜歡使用launchctl,因爲寫出整個plist要快得多,但我一直無法弄清楚如何讓它在殺死後重新啓動。我已經閱讀了手冊頁,但沒有找到任何內容。有沒有辦法?通常我用下面的命令:Launchctl Start Interval

launchctl submit -l somename -p /path/to/script -o output.txt -e errors.txt

但如果是任何時間間隔後殺死,不會重新啓動程序。

+0

你爲什麼要這麼做?如果我知道你確切的問題,可能有另一種解決方案。 – jamespick

+0

你是不是指'launchctl submit'?加載不會接受這些參數。在這種情況下,提交命令不支持間隔,因此您需要使用.plist版本。 – gaige

+0

@gaige對不起,我做到了 – 735Tesla

回答

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. 

,如果它出錯退出再次運行程序,設置保持活動的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秒。