unix-crontab

A crontab contains entries for a cron job. Entries in the crontab are separated by newline characters. Each crontab contains six fields separated by spaces as shown below:

Minute Hour Day Month Weekday Command

Each field is explained below:

Minute: 0 ~ 59

Hour: 0 ~ 23

Day: 1 ~ 31

Month: 1 ~ 12

Weekday: 0 ~ 6 (Sunday to Saturday)

Command: A shell file which you want to execute routinely or at a particular time. An example of a crontab job is shown below:

0 1 15 * * /backup.sh

This crontab entry will run the backup.sh file at 1 O’Clock on the 15th of every month. Remember, this command will be run as a root user. How about the situation where we want to run a particular shell from a user other than root? Lets look at the following example:

0 5 * * * su – informix -c "/opt/informix/Report/maintain.sh" > /dev/null 2>&1

This command will run the maintain.sh script as an informix user at 5AM on a daily basis.

If you are curious about the garbage at the end of the command, you need to look at the following explanation to it:

Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

In Unix & Linux systems, you can use the following commands to play with the crontab.

crontab –l : This will list the crontab entries.

crontab –e: This will edit the crontab, it will take you to vi editor and you can add or remove crontab entries.

Adnan Khurshid

Adnan Khurshid, the author of this article, has been working in a telecommunication sector since 2007. He has worked there as a VAS (Value Added Services) engineer and has excelled remarkably in the field. Working in this field has been his passion and he has always made efforts to keep himself up to date. Find more about him on LinkedIn

More Posts

Tags: , , , , , ,

1 Comment on All you need to know about Crontab

Leave a Reply