User Tools

Site Tools


cron

CRON

It is most important piece of command / daemon available on Linux / Unix system.
It has some nice capability which is not used but need to be aware of.

Special Usage

Following Special words replaces common conventional usage

@reboot        #Run once, at startup.
@yearly        #Run once a year, "0 0 1 1 *".
@annually      #(same as @yearly)
@monthly       #Run once a month, "0 0 1 * *".
@weekly        #Run once a week, "0 0 * * 0".
@daily         #Run once a day, "0 0 * * *".
@midnight      #(same as @daily)
@hourly        #Run once an hour, "0 * * * *".

Following variables you can define in cron as standard environment.

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

Usage

Most common Usage terms defined as below. <note>when you use “*” it consider all like “* * * * *” considered as every minute of 24/7</note>

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *               0,6           /bin/sshcheck

Also most of the time you might want to disable logging the output of commands or scripts you are running.
Which can be used as follow.

0 3 * * * bin/git_commit.sh >/dev/null 2>&1

Also some time you want to store output to file. it use standard notation for redirect.
For Append » For Overwrite >

35 0 * * * /bin/bash /home/kpatel/workspace/backup.sh >> output.txt 2>&1

Things need to know

  • When cron job is run from the users crontab it is executed as that user. It does not however source any files in the users home directory like their .cshrc or .bashrc or any other file. If you need cron to source (read) any file that your script will need you should do it from the script cron is calling. Setting paths, sourcing files, setting environment variables, etc.
  • If the users account has a crontab but no useable shell in /etc/passwd then the cronjob will not run. You will have to give the account a shell for the crontab to run.
  • If your cronjobs are not running check if the cron deamon is running. Then remember to check /etc/cron.allow and /etc/cron.deny files. If they exist then the user you want to be able to run jobs must be in /etc/cron.allow. You also might want to check if the /etc/security/access.conf file exists. You might need to add your user in there.
  • Crontab is not parsed for environmental substitutions. You can not use things like $PATH, $HOME, or ~/sbin. You can set things like MAILTO= or PATH= and other environment variables the /bin/sh shell uses.
  • Cron does not deal with seconds so you can't have cronjob's going off in any time period dealing with seconds. Like a cronjob going off every 30 seconds.
  • You can not use % in the command area. They will need to be escaped and if used with a command like the date command you can put it in backticks. Ex. `date +\%Y-\%m-\%d`

Things need to know pasted from

cron.txt · Last modified: 2017/07/02 02:18 by k2patel