1

I have a python script, and i'd like to make it run every 10 minutes, how can i do this? Thanks in advance.

0
14
  • Make the script executable by:

    chmod u+x /path/to/script.py
    

    Note that, you need a shebang (i.e. indicate interpreter in the first line of the script), for python2:

    #!/usr/bin/env python2
    

    For python3:

    #!/usr/bin/env python3
    
  • Open your cron table by

    crontab -e 
    
  • Add the following cron entry:

    */10 * * * * /path/to/script.py 
    

Note that, if the script is not executable you can indicate the interpreter on the go:

  • For python2:

    */10 * * * * /usr/bin/env python2 /path/to/script.py
    
  • For python3:

    */10 * * * * /usr/bin/env python3 /path/to/script.py
    
2
  • Can i do this with .sh files too? I'd like to combine 3 scripts into one.
    – NikoCodey
    Jul 15 '16 at 16:51
  • @NikoCodey Yes, you can..check my edits too..
    – heemayl
    Jul 15 '16 at 16:55
2

To avoid cron, you could also call your script in an infinite loop with a delay of 10 minutes.

If you want to launch the script again 10 minutes after the previous instance exited, use this:

while true ; do /PATH/TO/SCRIPT.PY ; sleep 10m ; done

However, if you want to launch the script every 10 minutes on the clock, no matter how long the last instance was running (or whether it still is running), you must execute the script in background and sleep in parallel by replacing the ; with an &:

 while true ; do /PATH/TO/SCRIPT.PY & sleep 10m ; done

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