Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 '14 at 21:31
The script is used to scraped Google results, so it needs to take some time due to IP overload. – Alex Apr 6 '14 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 '14 at 18:00
up vote 3 down vote accepted

In terminal:

while :; do ./script_name; done


while :; do python; done

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

For example:

while :; do python; 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 anywhere? – Alex Apr 6 '14 at 10:34
while [ 1 ]; do python; done You may add another commands if you want: while [ 1 ]; do python; command2; command3; etc; done Just split it with ; – user3439968 Apr 6 '14 at 16:29
Answer below and my answer it's shell scripting. Bash, sh, etc:… – user3439968 Apr 6 '14 at 16:36
Thanks @user3439968 - I have used the third instruction and it works like a charm. That is all I need. – Alex Apr 7 '14 at 18:50
You are welcome. Enjoy ) – user3439968 Apr 8 '14 at 0:57

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
    sleep 60
    date >> /path/to/logfile.txt
    echo "The script ended, and I restarted it after 60 seconds" >> /path/to/logfile.txt

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

Your Answer


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.