You probably came here because you need to set up a cronjob and need help with the scheduling syntax. Here are a few examples to help you out:
|Minute||Hour||Day of month||Month||Day of week||Explanation|
|30||8||*||*||*||Every day at 8:30|
|0||0||1||*||*||Every first day of the month at 0:00|
|0||2||*||*||1||Every Monday at 2:00|
|45||8||1||1||*||8:45 on the 1st of January|
|0||*/2||*||*||*||Every two hours|
|0||*||*||*||1,3,5||Every hour on Mondays, Wednesdays, and Fridays|
So, if you need to execute a command every day at 8:30, that is what you want to put into the crontab:
30 8 * * * echo “Hello world!”
If you want to play around with different expressions and understand what they do, a tool we can highly recommend is crontab.guru.
What is a Cron?
Cron is a tool to schedule jobs, often referred to as cronjobs. When a job will be executed is controlled by the scheduling expression, a string split into 5 parts:
- Day of the month
- Day of the week
Every minute, Cron can then match the current time against the scheduling expression to detect whether it should execute the command or not.
Making Changes to the Crontab
Setting up a new Cronjob is amazingly easy using the crontab:
This opens the crontab file with your favorite text editor. Do not worry if the file is completely empty, this just means there are no cronjobs scheduled yet.
So, let us schedule our first cronjob by adding a new line to the crontab:
* * * * * date >> /tmp/cronjob-output.txt
|Scheduling expression||Command to execute|
|*||*||*||*||*||date >> /tmp/ cronjob-output.txt|
|Run every minute…||…every hour…||…on every day of the month…||…on every month…||…on every day of the week…||…and append the current date to the file “/tmp/ cronjob-output.txt”|
Now, simply save the file and the new job is scheduled. Wait a few minutes and open the “/tmp/ cronjob-output.txt” file – you should see when the cronjob was executed:
Mon Mar 6 20:09:00 CET 2023 Mon Mar 6 20:10:00 CET 2023 Mon Mar 6 20:11:00 CET 2023
If we change the entry in the crontab to this:
*/2 * * * * date >> /tmp/cronjob-output.txt
And, after waiting for a few minutes, we can see that the command is now executed every two minutes instead of every minute:
Mon Mar 6 20:12:00 CET 2023 Mon Mar 6 20:14:00 CET 2023 Mon Mar 6 20:16:00 CET 2023 Mon Mar 6 20:18:00 CET 2023 Mon Mar 6 20:20:00 CET 2023
Cron Schedule Expressions
Here are a few more examples of Cron scheduling expressions:
Hourly from hour 8 to 17
0 8-17 * * * echo “Hello world”
Every Monday at 8am
0 8 * * 1 echo “It is Monday, 8am”
Daily on a specific time
30 15 * * * echo “Current time: 15:30”
Every 30 seconds
To make a cronjob run multiple times per minute, we need to set up multiple cronjobs scheduled for the same time but with different delays. In this case, to make them run every 30 seconds, the first job will be executed immediately, while the other one waits for 30 seconds:
* * * * * echo “First execution this minute” * * * * * (sleep 30; echo “Second execution this minute”)
Every first day of the month
0 0 1 * * echo “Hello, it is the first day of the month!”
Accessing Other User’s Contabs
In case you need to access a crontab of another user, you can do so by executing the crontab command as another user using the sudo command:
sudo crontab -e -u www-data
In this case, we can open the crontab of the www-data user. It is also possible to simply switch to the user using the su command, however this method does not work with disabled users or users without shell access.
If you want to learn more about users, especially about the root-user, check out this guide.