Linux - Scheduling tasks

October 2016

Introduction to the task scheduling

Task scheduling, is the ability for the OS to automatically perform a task (an application, control, ...) at regular intervals or at specific times. Generally this type of process is used to automate backups (called backup), especially at times when system usage is low.

Introduction to cron

The Unix-based systems (e.g Linux) have an application (actually a demon) for this purpose. It is called "cron".
  • Cron is based on a table referencing the tasks to be executed (as well as the time for execution of the tasks: year, month, day, hour and minute...etc).

Cron consists of:
  • a daemon: crond, that is to say, a memory-resident program that will automatically launch the tasks according to the cron table
  • a command: crontab to edit the table
    • cron (actually crond) is usually located in the /usr/sbin or /sbin in recent distributions.

To determine the location of crond on your linux distribution, type the following command:
  • whereis crond

The crontab command will only edit the table for the current user. This file is located in:
  • /var/spool/cron/crontabs/user

Thus for the root user, the cron table will be stored in:
  • /var/spool/cron/crontabs/root

The syntax

To edit the cron table, just run the following command:
  • crontab -e

This command will stat the Vi editor with the current table (the first launch of crontab).
Each entry in the table (each line) corresponds to a task at hand and is denoted as follows:
  • mm hh dd MMM DDD task > log

In this syntax:
  • mm represents the minutes (0-59)
  • hh represents hour (0 to 23)
  • dd is the day (1 to 31)
  • MMM represents the month (1 to 12) or abbreviated name of the month (JAN, FEB, MAR....)
  • JJJ is the abbreviated name of the day or the number corresponding to the day of the week (0 is Sunday, 1 is Monday, ...)
  • task represents a command or shell script to be executed
  • log is the name of a file in which to store the operations log. If the > log is not specified, cron automatically send a confirmation b email. To avoid this, simply use /dev/null.

For each unit of time (minute / hour / ...) the possible notations are possible:
- *: At each time unit
- 2-5: time units (2,3,4,5)
- */3: All three units of time (0,3,6, ...)
- 5,8: time units (5 and 8)

Examples of use

In our example we will create a log of the available free disk space (in the /tmp/log_df.txt file) using the df command at specified time intervals:
Every day at 23:30:
30 23 * * * df >>/tmp/log_df.txt

The first day of every month at 23:30:
30 23 1 * * df >>/tmp/log_df.txt

Every Monday at 22:28:
28 22 * * 1 df >>/tmp/log_df.txt

2nd to 5th of each month at 10:12
12 10 2-5 * * df >>/tmp/log_df.txt

All even-numbered days of the month at 23:59
59 23 */2 * * df >>/tmp/log_df.txt

It is also possible to automatically execute more complex commands using a shell script. As a first step, you will need to create a script. Then declare it as a task in the cron table.

The following shell script exports records of MySQL (the ccmusers table for example) in a file whose name is ccmusers followed by the date in the form day-month-year-hour-minute:
DATE=$(date +%d-%m-%Y-%H-%M)
/usr/local/mysql/bin/mysqldump -u root ccmusers > /home/backup/ccmusers${DATE}.sql

To make a daily backup (at 23:59) of the ccmusers table, just add the following entry in the cron table:
59 23 * * * /home/backup/backup.cmd >>/dev/null

Original document published on

Related :

This document entitled « Linux - Scheduling tasks » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.