如果一个进程死了,我如何编写bash脚本来重新启动它?我有一个python脚本,它将检查队列并对每个项执行操作:# checkqueue.pywhile True:
check_queue()
do_something()如何编写bash脚本,以检查它是否正在运行,如果没有,则启动它。大致如下伪代码(或者它应该做一些类似的事情)ps | grep?):# keepalivescript.shif processidfile exists:
if processid is running:
exit, all ok
run checkqueue.py
write processid to processidfile我从crontab打电话说:# crontab*/5 * * * * /path/to/keepalivescript.sh
3 回答
UYOU
TA贡献1878条经验 获得超4个赞
lf = open('/tmp/script.lock','w')if(fcntl.flock(lf, fcntl.LOCK_EX|fcntl.LOCK_NB) != 0): sys.exit('other instance already running')lf.write('%d\n'%os.getpid())lf.flush()
if [ `flock -xn /tmp/script.lock -c 'echo 1'` ]; then echo 'it's not running' restart. else echo -n 'it's already running with PID ' cat /tmp/script.lockfi
'other instance already running'
添加回答
举报
0/150
提交
取消