Saturday, 17 December 2016

Background

Cron is a daemon that runs in background and helps executing commands or set of commands at a predefined time. You can use to to schedule your commands at regular intervals. For example a batch job to pull all your unread mails.

Crontab

This will be per user. So each user can have their cron jobs and are stored in separate files called crontab files. To handle these the command used is crontab. The crontab file is often stored in

/var/spool/cron/crontabs/<user> (Unix/Slackware/*BSD),

/var/spool/cron/<user> (RedHat) or

/var/cron/tabs/<user> (SuSE),

but might be kept elsewhere depending on what Un*x flavor you're running. Though these files are in var they are not supposed to be edited directly. As mentioned earlier you should use crontab commands.

crontab [ -u user ] file

crontab [ -u user ] { -l | -r | -e }

Options are as follows -

The -l option causes the current crontab to be displayed on standard output.

The -r option causes the current crontab to be removed.

The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables. After you exit from the editor, the modified crontab will be installed auto‐matically. If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.

For more details see

man crontab

As mentioned in the usage of edit crontab option you need to first set your editor -

aniket@aniket-Compaq-610:~$ export EDITOR=vi

Creating a new cron job

Edit your crontab file with command -

aniket@aniket-Compaq-610:~$ crontab -e

Now your crontab file should be opened in vi. Enter following command, press enter and save your file.

*/1 * * * * date >> /tmp/log.txt 2>&1

This essentially puts current date every 2 mins in /tmp/log.txt file (combines standard output and error streams to stdout)

You should see following output -crontab: installing new crontab

Post saving you can check the contents of your crontab file with command -

crontab -l

You can now check /tmp/log.txt file for output -

Finally you can delete your crontab file using command -

crontab -r

Format of crontab file command line

Syntax is as follows -

minute hour day-of-month month day-of-week command

Possible values -

The asterisk (*) operator specifies all possible values for a field. e.g. every hour.

The comma (,) operator specifies a list of values. For eg every 2nd and 10th hour of the day.

The dash (-) operator specifies a range of values, Eg: "1-4", which is equivalent to "1,2,3,4".

The slash (/) operator, can be used to skip a given number of values. Eg "*/4" in the hour part of syntax would be equivalent to "0,4,8,12,16,20". "*" specifies 'every hour' but the "/4" means that skip 4 and try.

Now lets revisit the command we used -

*/1 * * * * date >> /tmp/log.txt 2>&1

It means execute command every minute, every hour, every day of month, every year, each day of a week.

There are also special string that you can use instead of above syntax -

NOTES

As mentioned earlier each user has his/her crontab file. If you want to do operations that require root permissions you need to use root crontab file. To use that you can execute

sudo crontab -e

In the /etc directory you will probably find some sub directories called

'cron.hourly',

'cron.daily', '

cron.weekly' and

'cron.monthly'. If you place a script into one of those directories it will be run either hourly, daily, weekly or monthly, depending on the name of the directory.

Cron will email to the user all output of the commands it runs, to silence this, redirect the output to a log file or to /dev/null.

Eg. */1 * * * * date >> /dev/null 2>&1

For Cron permissions these two files play an important role:

/etc/cron.allow - If this file exists, it must contain your username for you to use cron jobs.

/etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file does exist then, to use cron jobs, you must not be listed in the /etc/cron.deny file.