Description
This document outlines how to use cron
for scheduling events.
Prerequisites
Install cron
if it is not already setup on your system.
sudo apt update && sudo apt install cron
Basic Usage
Open the terminal and type,
crontab -e
This will open the crontab (cron configuration file) for your user account.
The first time you run this command it may give you the following prompt:
1 | no crontab for anthony - using an empty one |
I recommend selecting option 1.
Then you should see a crontab
file open on your screen that looks like this,
1 | # Edit this file to introduce tasks to be run by cron. |
Each line you add can define one command to run and its schedule.
The format structure is listed in the file as m h dom mon dow command
or spelled out thats,
minute (0-59)
hour (0-23)
day-of-month (1-31)
month (1-12)
day-of-week (0-7)
followed by the command
or /path/to/script
Examples
The following entry would execute a shell script (incremental-backup
) at 11:00 and 16:00 (twice) each day.
00 11, 16 * * * /home/user/bin/incremental-backup
The comma separated value in a field allows us to specify that the command needs to be executed at multiple times.
Administrator
Commands that normally run with administrative privileges should be added to the root
crontab
file.
To edit the root
crontab
type,
sudo crontab -e
Sources
https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804 ↩︎
https://stackoverflow.com/questions/22743548/cronjob-not-running ↩︎
https://bc-robotics.com/tutorials/setting-cron-job-raspberry-pi/ ↩︎
https://www.circuitbasics.com/how-to-write-and-run-a-shell-script-on-the-raspberry-pi/ ↩︎