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.

2 Answers2

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
    
heemayl
  • 91,753
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
Byte Commander
  • 107,489