我想用这个作业在 ubuntu 中设置一个 cron 作业
我有一个python网页抓取程序,程序终止后需要不断抓取。也就是说流程是这样的
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
谢谢
最佳答案
当程序运行时,在某处写入一个临时文件,并确保在程序终止时删除该文件。
然后在程序每次运行时测试文件是否存在。如果存在则退出。
运行.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。您也可以采取一些措施来防止或检测此类情况。
关于linux - 每次程序终止时运行的 cron 中的 Bash 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554572/