1

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

  • some other ideas http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at – aleksanderzak Mar 21 '14 at 12:09

1 Answers1

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.

Huang Tao
  • 2,254
  • 2
  • 26
  • 31
  • 1
    Create TMP_FILE using mktemp: TMP_FILE=$(mktemp -t myapp.XXX) echo $TMP_FILE /tmp/myapp.025 and use trap clean EXIT to cleanup temporary files: clean() { rm $TMP_FILE; }; trap clean EXIT – aleksanderzak Mar 21 '14 at 11:48
  • so if the program gets crashed or something, then will it effect the results of the program? its actually scrapping data from a website – user3418147 Mar 21 '14 at 12:00
  • is there any simpler method as an alternative? – user3418147 Mar 21 '14 at 12:01
  • Use trap to handle early exists/crashes and here you have some other ideas - http://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at – aleksanderzak Mar 21 '14 at 12:10