In ubuntu Linux,many times we need to run certain tasks regularly, such as every 10 minutes, at 2 o’clock in the morning every day, etc.
The tasks can be varied, but as data scientists, many of our tasks are based on python code.
A simple python job
Suppose we have a very simple python text that needs to be run at 2 AM every day. The python text is named test.py, and the code is as follows:
a=3 |
we could write a bash script,run.sh to run the above python job:
#!/bin/bash |
we need to make the run.sh to be excecutable:
chmod +x run.sh |
We then can run the script,implement the python code,got the answer of a+b and pipeline it to the file log.txt:
./run.sh |
Our job is now to figure out how to run.sh script file automatically。
Install crontab
If your ubuntu doesn’t have contab
install:apt-get install cron |
add crontab job
the command is:
crontab -e |
follow the hint and choose the edit environment you like, such as nano or vi.
If we want to run the job 2 am every day, the logic is like this:
0 2 * * * /home/user_name/run.sh |
If we want to run the job every 5 minutes, then rule is this:
*/5 * * * * /home/user_name/run.sh |
After save the above work, we have successfully scheduled the jobs.
Using the following command,
crontab -l |
we could confirm all the scheduled jobs including the one we just put in.