python - running the program after it terminates using cron -


i need set program in cron set in such way restarts everytime program terminated

why want job?

the program extracting information website using web scrapping , terminates when reaches point information up-to- date
part of python code

    sql = """select short_link properties short_link=%s"""             rows = cursor.execute(sql,(link_result))             print rows             if rows>=1:                 print "already present"                 sys.exit()             else:         query="""insert properties (published_date, title,price,bedroom,agency_fee, bathroom, size,prop_ref,furnished_status,rent_payment,building_info,amenities,trade_name,licence, rera_id,phone_info,short_link) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""         cursor.execute(query,(date_result, title_result, price_result, bedroom_result, agencyfee_result, bathroom_result, size_result, propertyref_result, furnished_result, rent_is_paid_result, building_result, amenities_result, tradename_result, licencenum_result, reraid_result, phone_result, link_result)) 

the script follows: run.sh

#!/bin/bash     path=$path:/bin:/usr/bin      date +'%h:%m:%s started' >> /home/ahmed/desktop/log.txt      tmp_file=/tmp/i_am_running     [ -f $tmp_file ] && exit     touch $tmp_file      date +'%h:%m:%s starting python' >> /home/ahmed/desktop/log.txt     /usr/bin/python /home/ahmed/desktop/python.py     rm $tmp_file      date +'%h:%m:%s ended' >> /home/ahmed/desktop/log.txt 

the cron command using * * * * * /home/ahmed/desktop/run.sh

the log file follows:

15:21:01 started 15:21:02 starting python 15:22:02 started 15:23:01 started 15:24:01 started 15:24:30 ended 15:25:01 started 15:25:01 starting python 15:26:01 started 15:27:18 started 15:28:01 started 15:29:01 started 15:30:01 started 15:31:01 started 15:31:16 ended 15:32:01 started 15:32:01 starting python 15:33:01 started 15:34:01 started 

it seems program restarted before ended. log file should have starting program, started, ended, starting program, started, ended , on.

can guide me please? perhaps there changes need in bash script? how can set program starting, started, ended , on

the program restarted before ends because running cron job every minute of every hour of every day...

* * * * * /home/ahmed/desktop/run.sh 

what need infinite loop run program until finishes , start running again when loop finishes. running program every minute ok when know task not gonna take longer minute.

so do:

while(true){      path=$path:/bin:/usr/bin      date +'%h:%m:%s started' >> /home/ahmed/desktop/log.txt      # tmp_file=/tmp/i_am_running # don't need     # [ -f $tmp_file ] && exit # (don't need check if script running)     # touch $tmp_file # (you don't need create file)      date +'%h:%m:%s starting python' >> /home/ahmed/desktop/log.txt     /usr/bin/python /home/ahmed/desktop/python.py     # rm $tmp_file # (i commented don't need it)      date +'%h:%m:%s ended' >> /home/ahmed/desktop/log.txt      # can add 'sleep xx' delay script between executions in case don't      # want executed 1 time , execeution finishes.     # remember comment (add '#') or remove line of cron (crontab -e)  } 

what doing running instance of program every minute finish after running time takes:

15:21:01 started (start 1st program) 15:21:02 starting python (python script executed, called bash script) 15:22:02 started (cron runs 2nd program, not execute python, exit) 15:23:01 started (cron runs 3rd program, not execute python, exit) 15:24:01 started (cron runs program, not execute python, exit) 15:24:30 ended (end 1st program) 15:25:01 started (cron runs 5th program) 15:25:01 starting python (execute 5th program's python script, called bash script) 15:26:01 started (etc..) 15:27:18 started 15:28:01 started 15:29:01 started 15:30:01 started     15:31:01 started 15:31:16 ended 15:32:01 started 15:32:01 starting python 15:33:01 started     15:34:01 started 

as told best way in case infinite loop. if still prefer cron (which like, since infinite loops aren't best option, can set cron execute script every 10, 15, or 20 minutes better choice.

*/10 * * * * /home/ahmed/desktop/run.sh # exceute every 10 minutes */15 * * * * /home/ahmed/desktop/run.sh # exceute every 15 minutes 
  • remember if choose cron, need uncomment , keep lines check file (if file exists means program still executing). it's not probable 2 executions step each other since whole bash script (including python's) takes 3-4 minutes. 10 minute difference, it's enough.

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -