Linux and Unix Scripting

Learn Cron job on Linux – CronTab – Cron Task – Linux – bash script |


Linux Cron program allows you to save a list of tasks that will be executed periodically thru the Cron job rules. You can create a bash script to process your Cron task or only use the bash commands.

[[email protected] ~] # crontab audain.cron

First of all this is the options list:

  • -l : Displays the list of saved taskstyle=”padding-left: 30px;”>
  • -r : Deletes saved tasks
  • -u : Saves a schedule to a given user’s (when this one is not the current user)

Cron Task

The task job file definition can have any name, and you need to remember that each time you called the crontab , all your previous saved jobs will be overwrite.  The best practices in Linux cron job file, is that each line of your file have to be a defines a task. This file may look like this:

  • 10 * * * * run-parts /var/scripts/maintenance
  • 30 1 * * * /var/scripts/backup_daily
  • 45 2* 1 * / var / scripts / backup_weekly

You need to remember that the task structure has two parts: the periodicity of the task and the program that must be executed. Therefore, the periodicity is defined by 5 fields (a number or a “*”) and the task can be execute  thru a bash command or a bash script. Finally, cron will recognize simple bash commands, but it is better to put in your bash script the commands to execute, and specify this script in the cron job.


Most of all, if a line contains errors, they will be detected automatically when the tasks are saved ; however, if the scheduled task itself generates errors, crontab will not be able to detect them immediately. Consequently these errors will appear in the /var/mail user, you will find this file with the current user name in the folder  /var/mail/audain.

In addition, when you call crontab , the tasks are saved by the system in a file under the curent user name in the folder  /var/spool/cron/crontabs directory .

In the opposite the whole system task are defined and located  in /etc/crontab .

Cron job rules

First of all, the recorded task must be defined according to the following format:

[minute] [hour] [day of the month] [month] [day of the week] [command]

Is especially relevant that the “*” is used to specify that the command must be execute at each unit and numbers are used to restrict execution. 

In addition you will find below the way to launch a script every day of every month at 13:30.

30 13 * * *

Another possibility is to run a script once an hour (at first minute) throughout the month of February. 

1 * * 2 *

As an illustration below, you will find the table of the possible values:

Minute  : 0 to 59
Hour :    0 to 23
Day of the month   :  1 to 31
Month  : 1 to 12
Day of the week  :  0 to 7 (0 and 7 are both Sunday )

Cron job rules - CronTab - Cron Task - Linux - bash script

You will find bellow a solution with a dash, this script will run once an hour (at first minute) between 8am and 6pm, every day of year.

1 8-18 * * *

If we need to run a script once an hour (at the 10th minute) every Monday, Wednesday and Friday you will find bellow the solution with commas.

10 * * * 1,3,5

We can define intervals of several units in the same field with the slash:

For exemple, this script will be run once (at the first minute) every two hours each day.
1 * / 2 * * *

To conclude with the bash script

In fact there is no limit to the complexity of expressions you can use, as long as they respect the Cron job rules:
* / 10 * 1-15,20,25 * *


Source :