crontab – How it works?

crontab – How it works?

Introduction – cron is a daemon to execute scheduled commands (Vixie Cron). cron daemon has following options…

cron [-f] [-l] [-L loglevel]

Options
-f Stay in foreground mode, don’t daemonize.

-l Enable LSB compliant names for /etc/cron.d files

-L loglevel

Sets the loglevel for cron. The standard logging level (1) will
log the start of all the cron jobs. A higher loglevel (2) will
cause cron to log also the end of all cronjobs, which can be
useful to audit the behaviour of tasks run by cron. Logging
will be disabled if the loglevel is set to zero (0).

There are two type of cron job (crontab) –

1) User based cron job (crontab) –
2) System wide cron job (crontab) –

1] User based cron job (crontab) –

cron daemon searches its spool directory “/var/spool/cron/crontabs” for crontab files. After scheduling cron job by any user, the cron daemon will create file with that user name in it’s spool directory to store cron job for that user. Then cron daemon will load all cron jobs which are scheduled for each user into memory by reading contab file for each user from this spool directory. Please note that crontabs in this directory should not be accessed directly the crontab command should be used to access and update them.

root@arunbagul:~# ls -l /var/spool/cron/crontabs/
total 8
-rw——- 1 arun crontab 265 2008-03-31 19:22 arun
-rw——- 1 root crontab 317 2008-03-31 19:57 root
root@arunbagul:~#

root@arunbagul:~# ls -dl /var/spool/cron/crontabs/
drwx-wx–T 2 root crontab 4096 2008-03-31 19:57 /var/spool/cron/crontabs/
root@arunbagul:~#

root@arunbagul:~# grep crontab /etc/group
crontab:x:111:
root@arunbagul:~#

** root is the owner and crontab is the group owner of spool directory of cron daemon ie “/var/spool/cron/crontabs/”. The crontab files in spool directory is owned by respective user and group owner is “crontab”. please note the permission of spool directory and crontab files. As sticky bit is assigned on spool directory. only user can delete the crontab file(of course root also!).

For example – As shown below the crontab for user “arun” are stored in file “arun” in spool directory ie “/var/spool/cron/crontabs/arun” file.

root@arunbagul:~# cat /var/spool/cron/crontabs/arun

* 0 * * * /home/arun/update.sh

root@arunbagul:~#

How to start/stop/restart the cron daemon –

root@arunbagul:~# /etc/init.d/cron
* Usage: /etc/init.d/cron {start|stop|restart|reload|force-reload}
root@arunbagul:~#

root@arunbagul:~# /etc/init.d/cron start
* Starting periodic command scheduler crond [ OK ]
root@arunbagul:~#

root@arunbagul:~# /etc/init.d/cron restart
* Restarting periodic command scheduler crond [ OK ]
root@arunbagul:~#

root@arunbagul:~# /etc/init.d/cron stop
* Stopping periodic command scheduler crond [ OK ]
root@arunbagul:~#

NOTE – No need to reload the cron daemon after modifying the crontab file by any user.

2] System wide cron job (crontab) –

System wide setting for crontab is defined in “/etc/crontab” file and in “/etc/cron.d/” directory. The intended purpose of this feature is to allow packages that require control of their scheduling. The packages can add there crontab files to /etc/cron.d directory and such files should be named after
the package that supplies them. For example the crontab “/etc/cron.d/php5” is set by php5 package. see below… But this is not the case alway! You can use this features for customized scheduling!!

root@arunbagul:~# ls -l /etc/cron.d/
total 8
-rw-r–r– 1 root root 244 2007-03-05 12:08 anacron
-rw-r–r– 1 root root 456 2007-10-05 05:06 php5
root@arunbagul:~#

root@arunbagul:~# cat /etc/cron.d/php5
# /etc/cron.d/php5: crontab fragment for php5

# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -r -0 rm
root@arunbagul:~#

root@arunbagul:~# cat /etc/crontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts –report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.monthly )
#
root@arunbagul:~#

a) Hourly crontab(job) –

Setting for hourly crontabs is defined /etc/crontab file . Let’s see what is there in above /etc/crontab file. First and second line in /etc/crontab file defines SHELL and executable PATH variables respectively and third line is comments.

Now we will see fourth line…

17 * * * * root cd / && run-parts –report /etc/cron.hourly

cron daemon will run the command “cd / && run-parts –report /etc/cron.hourly” at 17th minute of every hour as “root” owner. Lets see what this command is doing. “&&” operator means that if first command is successful then only execute the second command (see example below) ie “&&” is logical AND. The first command is “cd /” which will set pwd as /. Once this command is successful, next command “run-parts –report /etc/cron.hourly” will be executed..

root@arunbagul:~# ls -l && pwd
total 48
drwxr-xr-x 2 root root 4096 2006-02-25 05:19 Desktop
drwxr-xr-x 2 root root 4096 2006-02-25 05:19 Documents
drwxr-xr-x 2 root root 4096 2006-02-25 05:19 Music

/root

root@arunbagul:~#

root@arunbagul:~# ls -M && pwd
ls: invalid option — M
Try `ls –help’ for more information.
root@arunbagul:~#

command (1) run-parts – is the command which runs all the executable files , script or programs in given directory!.

–test print the names of the scripts which would be run, but don’t actually run them.

–list print the names of the all matching files (not limited to executables), but don’t actually run them. This option cannot be used with –test.

-v, –verbose
print the name of each script to stderr before running.

–report
similar to –verbose, but only prints the name of scripts which produce output. The script’s name is printed to whichever of stdout or
stderr the script first produces output on.

–reverse
reverse the scripts’ execution order.

–exit-on-error
exit as soon as a script returns with a non-zero exit code.

Now we will see what this command is doing …

root@arunbagul:~# run-parts –list /etc/cron.hourly/
/etc/cron.hourly/arun
/etc/cron.hourly/autocommit
/etc/cron.hourly/backup
/etc/cron.hourly/download
/etc/cron.hourly/ftp
root@arunbagul:~#

so all above script will be run by crontab hourly. –list option will list the script which would be run by crontab hourly and –report option will run the all scripts within given directory…

* This is how hourly schedule jobs are executed/run by cron tab.

b) Daily, Weekly and Hourly crontabs –

Setting for daily, weekly and monthly crontabs is defined in /etc/crontab file. Now we will see 5th, 6th and 7th line of /etc/crontab file…

25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.monthly )

* crontab (cron daemon) will execute/run the script “test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.daily )” at 6:25 AM on every day, as this is daily job.

*cron will run this script “test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.weekly )” at 6:47 AM on every sunday as this is weekly job.

* cron will run this script “test -x /usr/sbin/anacron || ( cd / && run-parts –report /etc/cron.monthly )” at 6:52 AM on 1st day of every month as this is monthly job.

Now we will see what is this command doing – “||” operator is act as logical OR, means if first script is successful then second script would not be executed or run. and vice versa. let’s see one example here….

root@arunbagul:~# ll || pwd
total 48
drwxr-xr-x 2 root root 4096 2008-04-03 23:23 Desktop
drwxr-xr-x 2 root root 4096 2006-02-25 05:19 Documents
-rwxr-x— 1 root root 266 2008-02-15 09:48 myscript.sh

root@arunbagul:~#

root@arunbagul:~# ll -M || pwd
ls: invalid option — M
Try `ls –help’ for more information.

/root

root@arunbagul:~#

** Now we will check first part of command ie “test -x /usr/sbin/anacron“. ‘test‘ is the command which is used to check ‘file’ types and compare values. Here “-x” options check whether file exist or not and is file executalble (executable permission) or not?.. see below

root@arunbagul:~# test -x /usr/sbin/anacron
root@arunbagul:~#

root@arunbagul:~# echo $?
0
root@arunbagul:~#

So “/usr/sbin/anacron” file exist and it is executable that’s why command is successful so second part of command ie “( cd / && run-parts –report /etc/cron.weekly )” will NOT be executed as first part of command is successfully executed!

root@arunbagul:~# run-parts –list /etc/cron.weekly
/etc/cron.weekly/0anacron
/etc/cron.weekly/man-db
/etc/cron.weekly/popularity-contest
/etc/cron.weekly/sysklogd
root@arunbagul:~#

root@arunbagul:~# run-parts –test /etc/cron.weekly
/etc/cron.weekly/0anacron
/etc/cron.weekly/man-db
/etc/cron.weekly/popularity-contest
/etc/cron.weekly/sysklogd
root@arunbagul:~#

command (2) anacron – runs commands periodically. Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron, it does not assume that the machine is running continuously. Hence, it can be used on machines that aren’t running 24 hours a day and to control daily, weekly, and monthly jobs that are usually controlled by cron.

When executed, Anacron reads a list of jobs from a configuration file, normally “/etc/anacrontab“. This file contains the list of jobs that Anacron controls. Each job entry specifies a period in days, a delay in minutes, a unique job identifier, and a shell command. For each job, Anacron checks whether this job has been executed in the last n days, where n is the period specified for that job. If not, Anacron runs the job’s shell command, after waiting for the number of minutes specified as the delay parameter. After the command exits, Anacron records the date in a special timestamp file for that job, so it can know when to execute it again. Only the date is used for the time calculations. The hour is not used. When there are no more jobs to be run, Anacron exits. Anacron only considers jobs whose identifier, as specified in the anacrontab matches any of the job command-line arguments. The job arguments can be shell wildcard patterns (be sure to protect them from your shell with adequate quoting). Specifying no job arguments, is equivalent to specifying “*”.

** “/var/spool/anacron” is the spool directory used by Anacron for storing timestamp files.

-f Force execution of the jobs, ignoring the timestamps.

-u Only update the timestamps of the jobs, to the current date, but don’t run anything.

-s Serialize execution of jobs. Anacron will not start a new job before the previous one finished.

-n Run jobs now. Ignore the delay specifications in the /etc/anacrontab file. This options implies -s.

-d Don’t fork to the background. In this mode, Anacron will output informational messages to standard error, as well as to syslog. The out‐
put of jobs is mailed as usual.

-q Suppress messages to standard error. Only applicable with -d.

** Check what is there in /etc/anacrontab file –

root@arunbagul:~# cat /etc/anacrontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# These replace cron’s entries
1 5 cron.daily nice run-parts –report /etc/cron.daily
7 10 cron.weekly nice run-parts –report /etc/cron.weekly
@monthly 15 cron.monthly nice run-parts –report /etc/cron.monthly

root@arunbagul:~#

The format of “/etc/anacrontab” file is ..

Field No Field
——– —————-
(1) Period in days
(2) Delay in minutes
(3) Unique job identifier
(4) Shell command

* Daily – The period is 1 day , delay is 5 min and command is “nice run-parts –report /etc/cron.daily”.
* Weekly – The period is 7 days , 10 min delay time and command is “nice run-parts –report /etc/cron.weekly”
* Monthly – The period is one month and 15 min as delay time

Please read the man page of ‘nice’ command and we know what this “run-parts” command is doing…

root@arunbagul:~# run-parts –list /etc/cron.weekly
/etc/cron.weekly/0anacron
/etc/cron.weekly/man-db
/etc/cron.weekly/popularity-contest
/etc/cron.weekly/sysklogd
root@arunbagul:~#

root@arunbagul:~# run-parts –test /etc/cron.weekly
/etc/cron.weekly/0anacron
/etc/cron.weekly/man-db
/etc/cron.weekly/popularity-contest
/etc/cron.weekly/sysklogd
root@arunbagul:~#

root@arunbagul:~# run-parts –list /etc/cron.monthly/
/etc/cron.monthly//0anacron
/etc/cron.monthly//proftpd
/etc/cron.monthly//scrollkeeper
/etc/cron.monthly//standard
root@arunbagul:~#

root@arunbagul:~# run-parts –test /etc/cron.monthly/
/etc/cron.monthly//0anacron
/etc/cron.monthly//proftpd
/etc/cron.monthly//scrollkeeper
/etc/cron.monthly//standard
root@arunbagul:~#

** Check what is there in spool directory “/var/spool/anacron/” –

root@arunbagul:~# cd /var/spool/anacron/
root@arunbagul:/var/spool/anacron# ll
total 12
-rw——- 1 root root 9 2008-04-03 23:35 cron.daily
-rw——- 1 root root 9 2008-03-13 14:32 cron.monthly
-rw——- 1 root root 9 2008-04-04 00:19 cron.weekly
root@arunbagul:/var/spool/anacron#

root@arunbagul:/var/spool/anacron# cat cron.daily
20080403
root@arunbagul:/var/spool/anacron#

root@arunbagul:/var/spool/anacron# cat cron.monthly
20080313
root@arunbagul:/var/spool/anacron#

root@arunbagul:/var/spool/anacron# cat cron.weekly
20080404
root@arunbagul:/var/spool/anacron#

Summary of article –

Scheduling in GNU/Linux or Unix is achieved by crontab (cron daemon) with the help of anacron and run-parts commands!

Thank you,

Arun Bagul

Similar Posts:

One Reply to “crontab – How it works?”

  1. You might be interested in this new resource for users wanting to do cron job scheduling.

    Cron Sandbox at HxPI is an interactive webpage where you can play with crontab command strings.

    Enter your ‘m h D M Dw’ parameters and immediately see all the times cron would run your job in the coming days/weeks. 

    http://www.hxpi.com/cron_sandbox.php

Leave a Reply

Your email address will not be published.