How to Set cron job in Linux Server step by step

How to Set cron job in Linux Server step by step

What is crontab?

The crontab is a list of commands that run on a regular schedule, means crontab executes task on specified time. Crontab word combines from “crontable” because it uses for a job scheduler to perform some task. The crontabcommand creates a crontab file containing commands and instructions for the cron daemon to execute. Normal users can use the crontab command to manage their jobs. This command can be called in four different ways:

Command Intended use
crontab  -l List the job for current user.
crontab  -e Edit jobs for the current user.
Crontab  -r Remove all jobs for the current user.
Crontab <filename> Remove all jobs, and replace with the jobs read from <filename>. If no file is specified, stdin will be used.

Each entry in a crontab file consists of six fields, specifying in the following order:
The fields are separated by spaces or tabs. The first five fields are integer patterns and the sixth field is the command to execute. The following table briefly describes each of the fields:

Sequence of Commands

Job Structure

Each job consist of six fields detailing when and what should be executed. When first five fields of match the current date and time, then in the last field will be executed. Now we go with these fields step by step.

  1. Minute  2. Hours  3. Day-of-Month   4. Month   5. Day-of-Week    6. Command

Field       Value     Description
minute     0-59     The exact minute that the command sequence executes
hour        0-23     The hour of the day that the command sequence executes
day          1-31     The day of the month that the command sequence executes
month     1-12     The month of the year that the command sequence executes
weekday     0-6     The day of the week that the command sequence executes (Sunday = 0, Monday = 1, Tuesday = 2, and so forth)
command     Special        The complete sequence of commands to execute. The command string must conform to Bourne shell syntax. Commands, executable (such as scripts), or combinations are acceptable.


Important Note: When “Day-of-Month” and “Day-of-Week” fields are both other than * (* indicates all valid), then the command will be executed when either of these two fields match. HR mostly asks following interview question for fresher.

Linux System Administrator Interview Questions and Answers for Fresher

cron job Exmples:

0    5    3   10    *   /usr/local/bin/report

Execute the command /usr/local/bin/report at exactly 5 a.m, on March 10th, every year.

*/10   10-22   *    Aug    3  echo “Send this Email”

Send an email containing the word “Send this Email” to the owner of this job, every ten minutes between 10 a.m. and 10 p.m. (Please clear this 22 means 10 o’clock at night, please refer above table) on every March (3 means March) in August.