Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I would like to run a Python script each time it finishes and I think I can do this with cron but I am not sure how to do it.

The idea is each time the Python scripts ends, it will have to run again and again.

I have searched for answers here but I can't find something similar to this.

share|improve this question
1  
Why not make the script loop rather than exit or make it run when something happens e.g. a directory changes depending on what it is looking for –  Mark Apr 5 at 21:31
    
The script is used to scraped Google results, so it needs to take some time due to IP overload. –  alexgarciab Apr 6 at 16:04
    
So it could loop and set launched up to restart it if it crashes (the latter would work even if it ends normally) –  Mark Apr 6 at 18:00
add comment

2 Answers 2

up vote 3 down vote accepted

In terminal:

while :; do ./script_name; done

or

while :; do python programm.py; done

If you want add some sleep time before run program again use 'sleep' command:

For example:

while :; do python programm.py; sleep 90; done

in this example cycle will sleep 90 second before run again you program

share|improve this answer
    
Do I have to write something like: python programm.py anywhere? –  alexgarciab Apr 6 at 10:34
    
while [ 1 ]; do python programm.py; done You may add another commands if you want: while [ 1 ]; do python programm.py; command2; command3; etc; done Just split it with ; –  user3439968 Apr 6 at 16:29
    
Answer below and my answer it's shell scripting. Bash, sh, etc: developer.apple.com/library/mac/documentation/opensource/… –  user3439968 Apr 6 at 16:36
    
Thanks @user3439968 - I have used the third instruction and it works like a charm. That is all I need. –  alexgarciab Apr 7 at 18:50
    
You are welcome. Enjoy ) –  user3439968 Apr 8 at 0:57
add comment

Cron is a scheduler, it's good if you want your script to run every 5 minutes, or every 3rd Wednesday etc, but for an arbitrary restart upon completion which could (I presume, you don't indicate what the Python script is doing, how long it takes or if the time to completion is variable etc) be essentially random it's not the best choice.

You can either insert some logic into the script directly, or you could simply wrap the script with some Shell Scripting logic as per @user3439968's answer, my version is essentially the same, but I've shown it a little more verbose with options for keeping an eye on the restarts etc. You can type this into pretty much any command line shell, simplest to just open Terminal and type it straight into it

while true
do
    /path/to/script.py
    sleep 60
    date >> /path/to/logfile.txt
    echo "The script ended, and I restarted it after 60 seconds" >> /path/to/logfile.txt
done

In the example above the restart isn't immediate but waits 60 seconds, then appends the current date and a message to a log file on each restart. You can put any control code after the initial run of the Python script and the done line, even get it to mail you that it's restarted etc.

Because you don't specify what "true" is, it can never be false, so essentially everything between do and done is looped over and over until it receives a suitable interrupt signal like a CTRL-c or a kill -9 PID type command from another command line session.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.