簡體   English   中英

如何定期重啟終端程序?

[英]How to restart a program in terminal periodically?

我正在從終端(在OS X Mavericks中)調用一個程序,比方說myprogram ,但是有時由於我無法控制的外部問題而卡住了。 這往往大約每半小時發生一次。

myprogram基本上必須執行大量小的子任務,這些子任務保存在每個新執行中都讀取的文件中,因此無需從頭開始重新計算所有內容。

我想通過以下方式終止並再次重新啟動程序,從而完全自動化重新啟動程序:

  1. 啟動程序。
  2. 30分鍾后將其殺死(該程序可能會卡住)。
  3. 重新啟動它(返回到步驟1)。

有關如何執行此操作的任何想法? 我對bash腳本的了解並不十分准確...

以下腳本可以用作myprogram的包裝腳本

#!/bin/bash
while true                        #begin infinite loop (you'll have to manually kill)
do
  ./myprogram &                   #execute myprogram and background
  PID=$!                          #get PID of myprogram
  sleep 1800                      #sleep 30 minutes (30m might work as parameter)
  kill -9 $PID                    #kill myprogram
done

你可以使用一個包裝,但是,一個無限循環不是最佳的解決方案。 如果您希望在計時器上是否重新啟動程序,取決於退出代碼並且在OS X上,則應使用launchd配置(xml屬性列表)文件,並使用launchctl加載它們。

 KeepAlive <boolean or dictionary of stuff>
 This optional key is used to control whether your job is to be kept continuously running or to let
 demand and conditions control the invocation. The default is false and therefore only demand will start
 the job. The value may be set to true to unconditionally keep the job alive. Alternatively, a dictio-nary dictionary
 nary of conditions may be specified to selectively control whether launchd keeps a job alive or not. If
 multiple keys are provided, launchd ORs them, thus providing maximum flexibility to the job to refine
 the logic and stall if necessary. If launchd finds no reason to restart the job, it falls back on
 demand based invocation.  Jobs that exit quickly and frequently when configured to be kept alive will
 be throttled to converve system resources.

       SuccessfulExit <boolean>
       If true, the job will be restarted as long as the program exits and with an exit status of zero.
       If false, the job will be restarted in the inverse condition.  This key implies that "RunAtLoad"
       is set to true, since the job needs to run at least once before we can get an exit status.

...

ExitTimeOut <integer>
     The amount of time launchd waits before sending a SIGKILL signal. The default value is 20 seconds. The
     value zero is interpreted as infinity.

欲了解更多有關發射和發射的信息,請訪問:

  1. https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
  2. https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM