问题
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