Crontab Syntax and Tutorial: Understand Cronjobs on Linux

Learn how to Set Up and use Cronjobs with crontabs on Linux systems.

Cron jobs can be used to automate processes on Unix and Linux systems and execute them recurrently at a specific time. These processes can be individual commands, shell scripts, programs, PHP and other script language scripts or even a string of Linux commands. For example, backups that are supposed to happen weekly, daily or hourly are usually executed by cronjob.

Crontab

Crontab is the table in which the individual cronjobs are defined and configured. The table contains per line the time and the command sequence that is to be executed. The term crontab is composed of the Greek chronos (time) and Latin tabula (the table, the board).

At the same time crontab is also the program with the help of which one can edit the crontabs. To edit the contab, the following command must be entered:

crontab -e

Crontab Syntax

Each cronjob has the following format:

* * * * command to be executed.
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ └────── day of the week (0-7, Sunday is 0 or 7).
│ │ └──────── month (1-12)
│ │ └──────── day (1-31)
│ └────────── hour (0-23)
└──────────── minute (0-59)

An asterisk * means execution will always occur, i.e., every minute, hour, day, month, or day of the week. To keep the individual digits apart, the following diagram helps:

1 2 3 4 5 Command
1 = minute (0-59)
2 = hour (0-23)
3 = day (0-31)
4 = month (1-12)
5 = day of the week (0-7, Sunday is 0 or 7)
Command = The command to be executed.

For the first five digits, i.e. the line values, the following additional options are possible:

* = execution always (at every..)
*/n = execution of all n
n,x,y = execution at/on n, x and y

Cronjob examples

For example, to run the backup every night at 5am, you would create the cronjob as follows:

0 5 * * /usr/bin/backup.sh

To play a sound every 10 minutes could look like this:

*/10 * * * /usr/bin/play_sound.sh

Sending a reminder mail at 8 and at 17 o'clock goes like this:

0 8,17 * * /usr/bin/send_reminder_mail.sh

Sending a mail on a certain day, for example on 31.12. at 23:59, could look like this:

59 23 31 12 * (echo "Let's go get the rockets" | mail -s "About to pop" user@domain.xy)

Handling Cronjobs Output

By default, the output of the cronjobs is sent by mail to the respective system user who set up the cronjob. To suppress this, you could redirect the output to a file or discard it completely with redirection to /dev/null:

Redirect Cronjob Output to log File

0 8,17 * * /usr/bin/script.sh >>/var/log/cron/send_reminder_mail 2>&1

Discard Cronjob Output

0 8,17 * * /usr/bin/script.sh >/dev/null 2>&1

2>&1 means that both normal output and errors are redirected to the previously specified file.

Files and Directories

In addition to the crontab command, depending on the distribution, there are files that contain system-wide crontabs and can only be edited by the root user:

/etc/crontab

The system crontab file, in which a system user who is to execute the command must also be specified:

1 2 3 4 5 user command
1 = minute (0-59)
2 = hour (0-23)
3 = day (0-31)
4 = month (1-12)
5 = day of week (0-7, Sunday is 0 or 7)
User = User name of the user under which the command will be executed.
Command = The command to be executed.

Example:

0 8,17 * * root /usr/bin/script.sh >>/var/log/cron/send_reminder_mail 2>&1
0 3 * * wwwrun /usr/bin/webjobs_nighly.sh >>/var/log/cron/send_reminder_mail 2>&1

/etc/cron* directories

Another way to create cronjobs are the directories under /etc/cron*, where all contained files are executed at a specific time. The files in detail

/etc/cron.d/ = extensions to the /etc/crontab file, same syntax.
/etc/cron.daily/ = Once sometime daily.
/etc/cron.hourly/ = Once sometime hourly.
/etc/cron.monthly/ = Once sometime monthly.
/etc/cron.weekly/ = Once sometime weekly.

The last four are often used when a job needs to be done at a certain interval, but the exact time is irrelevant.

A tip for using the percent sign in crontabs

Sometimes it happens that - for example when using a date - you want to use the percent sign within a crontab command:

0 8,17 * * /usr/bin/script.sh >>/var/log/cron/script_$(date +%Y%m%d).log

Unfortunately, this will not work like this and Crontab refuses to run the cronjob with the following or similar error message:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `)'
/bin/sh: -c: line 1: syntax error: unexpected end of file

The reason for this is that percent signs in the 6th field of each Crontab line are generally translated to a newline. Quote from the Crontab man page:

The sixth field of a line in a crontab file is a string that is executed by the shell at the specified times. A percent character in this field (unless escaped by ) is translated to a NEWLINE character.

Only the first line (up to a `%‘ or end of line) of the command field is executed by the shell. Other lines are made available to the command as standard input. Any blank line or line beginning with a `#‘ is a comment and is ignored.

The solution is to escape the percent sign, hence prefix it with a backslash:

0 8,17 * * * /usr/bin/script.sh >>/var/log/cron/script_$(date +%Y%m%d).log

In this way, nothing stands in the way of using the date with a percent sign.

Crontab and done?

I hope, I could give a small insight into Cronjobs under Linux and be helpful to the one or other with the syntax. This article is also written with some self-interest, because I often have to think about the exact order in the crontab. Of course there are a lot of other tools like at, anacron, fcron and so on. However, Cronjob has worked very reliably for me, so I haven't considered other tools yet.

Let the crons work!

Enjoyed this post?

My goal with this blog is to help people to get started with developing wonderful software while doing business and make a living from it.

Subscribe here to get my latest content by email.

I won't send you spam. You can unsubscribe at any time.

© 2024 Headystack. All rights reserved.
👋