Maab S.
6min Read

Guide to Cron Jobs

cron jobs cover image

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 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.


Cron Basics

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 commands:

Command

Description

crontab -e 
Opens the cron file for editing. If no cron files exist on your system, a new one is created automatically. When you enter this command for the first time, you may be asked to choose your preferred editor (vi or nano) for crontab.
crontab -l
Shows you the contents of the existing cron file.
crontab -u username -l
Shows you the crontab entries for the specified user. You can only execute this command as a super user.
crontab -u username -e
Lets you edit the crontab of the specified user. Again, you need super user privileges to run this command.
crontab -r 
Removes the cron file and its entries. Note that this command will permanently remove the cron file from your system, without giving you a confirmation prompt, so be careful while executing it. Alternatively, you can use to 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:

/etc/cron.hourly/
Execute once an hour.
/etc/cron.daily/
Execute once a day.
/etc/cron.weekly/
Execute once a week.
/etc/cron.monthly/
Execute once a month.

Crontab Syntax

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

The 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 Operators

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-11 in 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. 4L would depict the last Thursday of a month.
  • Comma (,): Used to enter multiple values for a field. E.g. specifying 2,4 in 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. */3 in 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 15 in the date field.
  • Weekday (W): Used to depict the closest weekday from the specified time. E.g. 13W means 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#3 depicts the third Tuesday of the month.

Crontab Keywords

There are also some default keywords that you can use to schedule cron jobs, without having to use the numeric value format.

Command

Description

@reboot
Run the command/script once, at startup.
@hourly
Run the command/script once an hour.
@daily
Run the command/script at midnight, every day.
@midnight
Has the same effect as @daily, runs the command/script once a day, at midnight.
@weekly
Run the command/script once a week, on Sunday, at midnight.
@monthly
Run the command/script on the first date of every month.
@yearly
Run the command/script on the 1st of January, at midnight, every year.

Crontab Permissions

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.allow doesn’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.

Crontab entry

Description

32 20 * * 3 /path/to/script-1; /path/to/script-2
To run multiple commands/scripts in the same crontab entry, you can use the “;” operator.
*/5 * * * * /etc/app-monitor.sh
To run a monitoring script every 5 minutes.
0 3 * * 1 /etc/cleanup.sh
To run a cleanup script on every Monday at 3am.
30 04 01 */3 * /etc/trimonthly_cleanup.sh
To run a job after every 3 months, at 4:30am.
*/30 * * * * /etc/rotate_logs.sh
To rotate logs after every 30 minutes.
0 18 * * 1-5 /etc/cleanup_cache.sh
To perform cache cleanup at 6pm on all weekdays.
@reboot /etc/cleanup_cache.sh
To cleanup cache every time the system starts.
30 6 1,15 * * /etc/app-monitor.sh
To perform app monitoring at 6:30am, on the 1st and 15th of every month.
00 08-17 * * 1-5 /etc/check_db_health.sh
To check the health of database every hour, between 8am to 5pm, on all weekdays.
0 0 1 1 * /etc/yearly.sh
To execute a script once a year.
0 */6 * * * /etc/app-monitor.sh
To run a script after every 6 hours.
@weekly /etc/weekly_script.sh
To run a script once a week.
0 0 20 * * /etc/cleanup.sh
To execute a script on the 20th of every month.

The Author

Maab S.

Maab is an experienced software engineer who specializes in explaining technical topics to a wider audience.

More posts from Maab