Scheduling automated jobs on the raspberry pi using cron
Cron is a job scheduling tool in Unix based systems that you can use to schedule “jobs.” I often use it to launch Python scripts at specific intervals.
Set up an automated job in cron is actually really, really simple, but something that many people think is complex because of the format. Luckily, you can use something like Crontab Generator to generate the format for you.
Here are the steps for setting up a cron job:
- Use Crontab Generator to get the actual format of your scheduled job
- Go into the terminal line on your raspberry pi and type:
- Hit the ‘i’ key to “insert” text
- Copy over your line from the Crontab Generator
- Hit the Escape key
- Type “:w” without the quotes to save the file
- Type “:q” to exit
Here’s an example of a cron I run daily at 7:15am on an AWS Server. It activates my virtual environment first, then runs the script. It sends the output of the script to two specific files in my tmp folder:
15 7 * * * source /home/chase/miniconda/bin/activate chase_dev; python /home/chase/path/to/script.py >/tmp/printed_output 2>/tmp/error_output.err