Guide to Cron Jobs
Cron has been the staple of Linux automation for decades. It allows administrators to schedule the periodic execution of certain commands or scripts. The scheduled commands/scripts are known as cron jobs. They are a great way to automate mundane administrative tasks.
For example, you can create a cron job to copy application dumps to a different server, after every 12 hours. Or you can create a cron job to automatically rotate a log file, as soon as the clock hits midnight. Or you can even set up a cron job to execute a complicated, multi-step bash script, at any given time.
In the following guide, we will share everything you need to know about cron jobs. We’ll start with the basics, then talk about the syntax and keywords, and finish off with a few handy cron job examples. Let’s begin!
- What is a Cron Job?
- Cron Basics
- Crontab Syntax
- Crontab Operators
- Crontab Keywords
- Crontab Permissions
- Some Crontab Examples
What is a Cron Job?
Cron is a Linux utility that has been a part of the kernel since its inception. Cron is a daemon, i.e. an application that’s always running in the background. The cron daemon’s job is simple: keep checking the cron file for cron jobs and execute them when the time comes.
A cron file is a text file, typically located at a default location like
/etc/crontab. It contains a set of commands and their scheduling instructions, specified by the administrator. Using cron jobs, you can automate several activities like disk management, application monitoring, backup generation, and database administration.
Cron jobs are used by administrators, production engineers, and developers alike. Administrators can use them to automate routine activities, like system upgrade monitoring, vulnerability scanning, and cleanup routines. Production engineers can set them up to regularly check application logs, health, and memory utilization. Developers can install cron jobs to periodically clean up database tables and fetch changes from Git repositories.
There are two main types of cron files:
- The system crontab: This is the general crontab used to execute system-wide commands. Only users with root privileges can set up jobs in the system crontab.
- The user crontab: As the name indicates, this cron file is created at the user level. Any user with admin-like privileges can have one.
Let’s look at some of the basic
crontab -u username -l
crontab -u username -e
crontab -i, which also clears the crontab, but prompts you for confirmation before doing so.
In addition to using crontab, root users can also install automation scripts in the following directories for periodic execution:
Every entry in the crontab must comply with the following syntax:
* * * * * command/script
- The first * indicates the minute the command should run on. Possible values: 0-59.
- The second * is for the hour. Possible values: 0-23.
- The third * depicts the day of the month. Possible values: 1-31.
- The fourth * represents the month of the year. Possible values: 1-12.
- The last * shows the day of the week. Possible values: 0-6, where 0 represents Sunday and 6 represents Saturday.
The following image is a visual representation of the crontab format.
┏╍╍╍╍╍╍╍╍╍╍╍ min (0 - 59) ┃ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍ hour (0 - 23) ┃ ┃ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍ day of month (1 - 31) ┃ ┃ ┃ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍ month (1 - 12) ┃ ┃ ┃ ┃ ┏╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍╍ day of week (0 - 6) ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ * * * * * Command to execute
To define a cron job, simply replace the relevant asterisk with your desired value. For example, if you wish to run a bash script at 8:30 pm, on every Saturday of every month, you can use the following syntax:
30 20 * * 6 /etc/automate.sh
30 specifies the minutes,
20 means 8pm in the 24-hour notation, the next two values are asterisks, which indicate that the script must run on all days and all months, and
6 indicates that the script must only execute on Saturdays. Note that you can’t skip an asterisk, you can only replace it with a legitimate value.
Crontab also supports several operators which enable users to have more flexibility in defining cron jobs.
- Hyphen (-): Used to specify a range of values. E.g. to make a cron run only between the months of August and November, you can enter
8-11in the month field.
- Last (L): This operator is only allowed for the date of month, or day of week fields. When used on its own (L), it indicates the last date of the month (when specified in the date field), and Saturday (if specified in the day field). When it’s followed by another value, and used in the day field, it indicates the last X day of a week. E.g.
4Lwould depict the last Thursday of a month.
- Comma (,): Used to enter multiple values for a field. E.g. specifying
2,4in the day of the week field will run the task on every Tuesday and Thursday.
- Slash (/): Used when you want to skip certain values. E.g.
*/3in the hour field translates to running a script every 3 hours.
- Question mark (?): Only allowed in the date and day fields, this operator is used to indicate “no specific value”. E.g. if you want to run a command on the 15th, but don’t care which day it is, you can put a
?in the day field, and
15in the date field.
- Weekday (W): Used to depict the closest weekday from the specified time. E.g.
13Wmeans that the command will run on the nearest weekday to the 13th. If the 13th is a Sunday, the command will run on Monday the 14th.
- Hash (#): This operator is only allowed in the day of the week field. You can use it to indicate the nth X day of a month. E.g.
2#3depicts the third Tuesday of the month.
There are also some default keywords that you can use to schedule cron jobs, without having to use the numeric value format.
@daily, runs the command/script once a day, at midnight.
It’s important to restrict crontab access to only the users who require it. You can use the following two files to enforce this:
/etc/cron.allow: If this file exists, it should contain the names of the users who can access and edit crontab.
/etc/cron.deny: If the
/etc/cron.allowdoesn’t exist, but this one does, it should only include the usernames which are not allowed to view, create, or edit the crontab.
Some Crontab Examples
To finish off this guide, we will include some sample crontab entries that will let you get an even better idea of how the cron syntax works.
32 20 * * 3 /path/to/script-1; /path/to/script-2
*/5 * * * * /etc/app-monitor.sh
0 3 * * 1 /etc/cleanup.sh
30 04 01 */3 * /etc/trimonthly_cleanup.sh
*/30 * * * * /etc/rotate_logs.sh
0 18 * * 1-5 /etc/cleanup_cache.sh
30 6 1,15 * * /etc/app-monitor.sh
00 08-17 * * 1-5 /etc/check_db_health.sh
0 0 1 1 * /etc/yearly.sh
0 */6 * * * /etc/app-monitor.sh
0 0 20 * * /etc/cleanup.sh
- The Beginners Guide to iptables
- A Letter to DigiServ Clients
- 12 Things Every Business Website Needs
- How To Use The Grep Command
- Site Builder Part 2: Overview of Functions and Features
- Guide to Cron Jobs
- Landing Page SEO Best Practices and Tips for Success
- Price Increase 2022
- 50+ GIT Commands with Downloadable Cheat Sheet
- 5 Ways to Stay on Budget with AWS