From TBP Wiki
Jump to: navigation, search

Cron is daemon to execute scheduled commands. Cron should be started from /etc/rc.d/init.d or /etc/init.d.

Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; The founded crontabs are loaded into memory. Cron also searches for /etc/anacrontab and the files in the /etc/cron.d directory, which are in a different format (see crontab(5) ). Cron examines all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists). Job output can also be sent to syslog by using the -s option.

There are two ways, how the changes are checked in crontables. The first is checking the modtime of file and the other is using inotify support. You can find out which of them are you using, if you check /var/log/cron where is (or isn't) inotify mentioned after start of daemon. The inotify support is watching for changes in all crontables and touch the disk only in case that something was changed.

In other case cron checks each minute to see if its crontables modtime have changes and reload those which have changes. There is no need to restart cron after some of the crontable is modified. The modtime option is used also when inotify couldn't be initialized.

Cron is checking those files or directories: /etc/anacrontab system crontab is usually for running daily, weekly, monthly jobs. /etc/cron.d/ where are system cronjobs stored for different users. /var/spool/cron that's mean spool directory for user crontables.

Note that the crontab(1) command updates the modtime of the spool directory whenever it changes a crontab.

How to run a cron every second

   *    *    *    *    *       echo `date -I`  >> /tmp/cron.log
   *    *    *    *    *       sleep 1 ; echo `date -I`  >> /tmp/cron.log
   *    *    *    *    *       sleep 2 ; echo `date -I`  >> /tmp/cron.log
   *    *    *    *    *       sleep 3 ; echo `date -I`  >> /tmp/cron.log


   *    *    *    *    *       sleep 59 ; echo `date -I`  >> /tmp/cron.log