cron中bash程序运行每次程序终止 [英] Bash program in cron that runs everytime the program is terminated

查看:122
本文介绍了cron中bash程序运行每次程序终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置在Ubuntu cron作业这份工作

I want to set a cron job in ubuntu with this job

我有需要的程序终止后连续报废蟒蛇webscraping程序。换言之,流动是这样的

I have a python webscraping program which needs to be scrapped continuously after the program is terminated. In other words the flow is like this

If program is terminated, set the cron job again (until infinity in cron's method)

something like * * * * * /python.py (but only when the python.py is terminated/finished)

有人可以指导我写一个bash的程序,做这份工作吗?该计划是python.py

Can someone guide me to write a bash program that does this job? The program is python.py

感谢

推荐答案

当程序运行时,写一个临时文件的地方,并确保该程序终止时,该文件将被删除。

when the program runs, write a temp file somewhere, and make sure this file is deleted when the program terminates.

然后测试文件是否存在每次程序运行时间。如果退出它的存在。

then test if the file exists every time the program runs. exit if it's there.

run.sh

TMP_FILE=/tmp/i_am_running
[ -f $TMP_FILE ] && exit
touch $TMP_FILE
./python.py
rm $TMP_FILE

在crontab,请拨打本 run.sh 而不是 python.py

记住,如果脚本退出早期(被杀死,等)和tmp文件停留在文件系统中,它不会再次运行python.py。有事情可以做,以prevent或发现类似的情况了。

keep in mind that if the script exits early (get killed, etc) and the tmp file stays in the file system, it won't run python.py again. there's things you can do to prevent or detect situations like that too.

这篇关于cron中bash程序运行每次程序终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆