Cron is used to schedule tasks at fixed intervals. We are going to use this to run my MonitorHTML python script since I would like it to run 3 times a day. We are going to be using the raspberrypi.org tutorial to set this up.
sudo apt-get install gnome-schedule
Pick nano as your editor
Add a task
Here is the format
* * * * * command to execute ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0) │ │ │ └────────── month (1 - 12) │ │ └─────────────── day of month (1 - 31) │ └──────────────────── hour (0 - 23) └───────────────────────── min (0 - 59)
Cron can take inputs in a few different ways. Let’s take a look at some examples in the minute field.
- * would execute every minute
- */5 would execute every 5 minutes (this can be replaced with whatever you want)
- 20,30,45 would execute on the 20th, 30th, and 45th minute.
We would like our script to run every day at 10 am, 2 pm, and 7 pm, so we would set the first argument to 0 to run on the 0th minute, then the second argument we would set to 10,14,19 for the hours we want, the rest of the arguments should be * because we want it to run every day, month, and day of the month
0 10,14,19 * * * python ~/MonitorHTML/MonitorHTML.py
Pretty simple right?
Other cron tasks
Listing scheduled tasks
View a cron log to make sure its running
grep cron /var/log/syslog
View that cron is running
ps aux | grep cron
sudo /etc/init.d/cron restart
Send output of crontab to a file
0 10,14,19 * * * python ~/MonitorHTML/MonitorHTML.py > ~/MonitorHTML/logs/backup.log 2>&1