我从终端(在OS X Mavericks中)调用一个程序,比如说myprogram
,但有时候由于我无法控制的外部问题而被卡住了。这种情况大约每半小时发生一次。
myprogram
基本上必须执行大量的小子任务,这些小子任务保存在每次新执行中读取的文件中,因此无需从头开始重新计算所有内容。
我希望通过以下方式杀死并重新启动程序来完全自动化重启程序:
关于如何做到这一点的任何想法?我对bash脚本的了解并不是很精确......
答案 0 :(得分:1)
以下脚本可用作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
答案 1 :(得分:1)
您可以使用包装器,但是,无限循环 不是最佳解决方案。如果您打算在计时器上重新启动程序,取决于退出代码并且在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.
有关launchd&amp ;;的更多信息plists访问: