Bash program in cron that runs everytime the program is terminated

梦想与她 提交于 2020-01-05 10:37:08

问题


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

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)

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

thanks


回答1:


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

in your crontab, call this run.sh instead of python.py.

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.



来源:https://stackoverflow.com/questions/22554572/bash-program-in-cron-that-runs-everytime-the-program-is-terminated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!