Cron jobs are an essential part of any Linux system. They allow administrators to schedule tasks to run at specified intervals, making it easy to automate repetitive or time-consuming tasks. Cron jobs can be scheduled to run by minute, hour, day of the month, month, day of the week, or any combination of these. This makes them very versatile and makes it possible to fine-tune the execution of tasks. For example, a cron job could be used to send out a daily report email or to back up a database every week. Cron jobs are extremely powerful and can make managing a Linux system much more manageable.
The most commonly used cron schedules are every 5, 10, or 15 minutes; each has its advantages and disadvantages. For example, running a cron job every 5 minutes means the task will be completed more often and uses more resources. On the other hand, running a cron job every 15 minutes means that the task will be completed less often but uses fewer resources. Ultimately, the best schedule for a cron job depends on the specific task that needs to be completed.
Table of Contents
First, before you create your 5, 10, or 15-minute commands from the examples, it has several operators for specifying multiple options when creating your cron command to get a better background on how Crontab works.
- comma (‘,’) – operator specifies a list of values, for example: “1,3,4,7,8”
- dash (‘-‘) – operator specifies a range of values, for example: “1-6”, which is equivalent to “1,2,3,4,5,6”
- asterisk (‘*’) – operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to ‘every hour’.
- slash (‘/’) – operator can skip a given number of values. For example, ” * /3″ in the hour time field is equivalent to “0,3,6,9,12,15,18,21”; “\*”
- specifies ‘every hour’ – the “/3” means that only the first, fourth, seventh…and such values given by “*” are used.
Please note the syntax of system-wide crontab files is slightly different than user crontabs. It contains an additional mandatory user field that specifies which user will run the cron job.
* * * * * <username> command(s)
Run a Cron Job Every 5 Minutes
Add the following line to your crontab file to run a cron job every 5 minutes.
*/5 * * * * command
As the command above states, the */5 means creating a list of all minutes.
Run a Cron Job Every 10 Minutes
Add the following line to your crontab file to run a cron job every 10 minutes.
*/10 * * * * command
As the command above states, the */10 means creating a list of all minutes.
Run a Cron Job Every 15 Minutes
Add the following line to your crontab file to run a cron job every 15 minutes.
*/15 * * * * command
As the command above states, the */15 means creating a list of all minutes.
By default, cron jobs cause an email to get sent to the user executing the command in crontab. If this is unnecessary and you wish to disable the email generation, add the following to the end of the cron job line.
> /dev/null 2>&1
> /dev/null 2>&1 means to send any standard output to /dev/null (the Linux trash can) and to redirect standard error (2) to the same place as the standard output (1). Incidentally, instead of dumping the output to /dev/null, it can also be appended to log files, for example.
> /var/log/crontab_output 2>&1
You can also disable email notifications for all of a particular user’s cronjobs by adding MAILTO=”” to their crontab.
Example of a cronjob in full with WordPress:
Comments and Conclusion
The tutorial has shown you have to create and run the three most commonly used cron jobs. Crontab is a fantastic way to schedule the execution of jobs, whether you want them to run daily, weekly, or monthly. When used correctly, it can take a lot of stress off your shoulders by automating tasks that would otherwise need to be done manually. We hope this guide has helped you better understand how Crontab works and how you can use it to your advantage. If not, feel free to reach out to us, and we’ll be happy to help clear things up for you.
For users who would like to create more cron times and schedules but are struggling, I suggest visiting the crontab.guru, as it’s one of the best cron schedule creators I have seen.