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 Answers
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
-
Can i do this with .sh files too? I'd like to combine 3 scripts into one. Jul 15, 2016 at 16:51
-
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