Scheduling tasks with cron and crontab in Linux

Scheduling tasks with cron and crontab in Linux

Cron is a time-based job scheduler used for automating and scheduling repetitive tasks, periodical collection of data, alerts, etc. The cron daemon or crond allows users to manage and run tasks automatically at specific time intervals. It is one of the key tools for scheduling tasks in Linux and Unix-like operating systems.

Cron

Cron is a command-line utility intended to schedule tasks or “cron jobs” to be run periodically and automatically, at a particular time and date. This task manager can be used for numerous purposes, such as:

  • Collecting system logs.
  • Creating backups.
  • Downloading email or files from the Internet.
  • Scheduling system maintenance.
  • Synchronizing files.

Tasks run in the background, without altering any Linux functionality. Users can set up an endless list of cron jobs to be executed as scheduled — every n-th minute, hour, day, month or year. The list of tasks is stored in a file called crontab.

Crontab

Crontab, short for “cron table”, is both a command and the file that contains the cron jobs that the cron daemon must periodically run. Every user has its own configuration file and manages his own scheduled jobs. The tasks listed in the crontab file contain the commands along with their date and time of execution. So that the Linux cron utility can run the scripts as scheduled when accessing and analyzing the file.

Cron jobs

Cron jobs have the following syntax:

min  h  d/m  mon  d/w  <command to execute>

They are formatted using the following fields:

FieldValues
min (minute)0-59
h (hour)0-23
d/m (day of the month)1-31
mon (month)1-12
d/w (day of the week)0-6 (Sunday to Saturday)
CommandThe Linux command the user wishes to schedule.

As well as diverse special characters, such as:

Special characterPurpose
*Select every possible value in a field.
*The asterisk can be used to fill a field without any specific value, as all fields must be filled when creating a cron job.
/Specify an interval of time.
,Select multiple values.
-Specify a range of time.

Examples of cron jobs scheduled in crontab

  • Run the command every day at half past midnight:
30  0  *  *  *  <command to execute>
  • Run the command every Saturday at 2:00AM:
0  2  *  *  6  <command to execute>
  • Run the command at midnight the first day of January:
0  0  1  1  *  <command to execute>
  • Run the command every five minutes from Monday to Friday:
*/5  *  *  *  1-5  <command to execute>

Crontab shortcuts

Moreover, in order to simplify commonly used schedules, there are some shortcuts or special commands available:

ShortcutSchedule
@rebootRun a task when the system restarts.
@yearly or @annuallyRun a task once a year.
@monthlyRun a task the first day of every month (0 0 1 * *).
@weeklyRun a task the first day of every week (0 0 * * 0).
@daily or @midnightRun a task every day at midnight (0 0 * * *).
@hourlyRun a task once an hour (0 * * * *).
Cron jobs cheat sheets: special characters and crontab shortcuts

Crontab command

In order to manage scheduled jobs, the following options can be added to the crontab command:

  • -e to edit the current crontab file.
  • -l to get a list of all the tasks within the current crontab file.
  • -r to permanently delete the current crontab file.
  • -u <user> <option> to run an option in another user’s crontab file.

Need help with servers and systems administration? Let’s talk.

Share it on Social Media!

Managed services

System administration services with direct access to specialized technical support.

DISCOVER MORE