Last updated: Sep, 2022
At Hostific, we love helping you learn new ways to take control of your website, so we’re pretty excited to tell you about cron jobs. Linux has lots of utilities that pack a punch, but cron jobs are definitely up there with the most useful Linux features.
When you schedule cron jobs, you can create batches of commands that will be launched at specific times. We usually use Cron Jobs to schedule important system-level commands, like deleting files periodically, or monitoring disk space, or running a backup. So, if you have a dedicated server, learning to use cron jobs can help you automate maintenance tasks and free up huge sections of your time.
In this guide, we’ll walk you through how to use Cron Jobs to schedule commands!
Table of Contents
Basic format of a cron job
This is the basic format of a typical cron job:
Minute (0-59) Hour(0-24) Day_of_month(1-31) Month(1-12) Day_of_week(0-6) Command_to_execute
To display the contents of the crontab file of the current user, do
$ crontab -l
If you want to edit the current user’s cron jobs, do
$ crontab -e
(If it’s your first time trying to edit, you’ll also be asked to choose an editor to edit the cron job.)
Using a crontab file
If you open a typical crontab file, you’ll see green writing against a black background.
NOTE: Each task must be defined through a single line that indicates when the task will be run and what command should be run for the task.
You can use the crontab file to easily schedule tasks (called ‘jobs’) based on specific timeframes. Each file is specific to a particular user, so any jobs created by one user will be invisible to another. This means if you share a server with other users, you’ll need a good communication system to make sure you don’t end up tying up resources by scheduling lots of jobs at the same time.
If you want to add tasks to your crontab, look for this line that defines the syntax:
m h dom mon dow command
How to schedule commands using a cron job
To schedule a command, use this format:
* * * * * command
Each asterisk (*) corresponds to a specific block of time:
Minute (0-59) Hour (0-24) Day (1-7) Month (1-12) Weekday (0-6) command
Examples of scheduling using cron jobs
- To run a cron job every minute, use this format:
* * * * * command
- To run a cron job every ten minutes, add this in your crontab file:
*/10 * * * * command
- To run a cron job every 25 minutes, add this:
*/25 * * * * command
- If you want a cron job to run once at ten past the hour, again at fifteen past the hour, and again at half past the hour, use this:
10,15,30 * * * * command
- To run a cron job every hour, use this:
0 * * * * command
This works because when the clock strikes an hour, we’re technically at minute 0 of that hour.
- To run a cron job every two hours, use this:
0 */2 * * * command
- To run a cron job every day, use this:
0 0 * * * command
This command will cause the cron job to run every day at 00:00.
- To run a cron job every day at a specific time, add the time to the command. So, for instance, if you want the cron job to run every day at 4 a.m., use this:
0 4 * * * command
- To run a cron job on a specific day, add the day to the command. For instance, to run a cron job every Sunday, use this:
0 0 * * SUN command
You could also use the number value of the day. (Remember that days are numbered from 0-6, with 0 being Sunday.) Using the number value, a command that’s scheduled to run every Sunday would look like this:
0 0 * * 0 command
This command will cause the cron job to run every Sunday at 00:00. If you want the job to run every Sunday at 4 a.m., use this:
0 4 * * 0 command
- To run a cron job every weekday, use this:
0 0 * * 1-5 command
- To run a cron job every month at 00:00, use this:
0 0 1 * * command
- To run a job on a specific month at a specific time, add the values to the command. For instance, to run a job at 17:20 every day in June, use this:
20 17 * 6 * command
- To run a job every three months, use this:
0 0 1 */3 * command
This cron job will run at 00:00 of the first day of every third month.
Other strings that can define a cron job:
- @reboot (This cron job will run once, every time you reboot.)
- @yearly (This cron job will run once a year.)
- @annually (This job will also run once a year.)
- @midnight (This is the same as @daily.)
Cron jobs look complicated, but when you know what each command means, it’s actually fairly intuitive to schedule tasks using cron jobs. If you want, you can feel free to bookmark this guide, so you can use it as a reference.
If you liked this guide on how to schedule cron jobs, check out our other posts on how to manage your website easily and effectively!