Guide to Unix/Explanations/Scheduling Jobs

From Wikibooks, open books for an open world
< Guide to Unix‎ | Explanations
Jump to: navigation, search

Scheduling with Cron[edit]

Cron is an automated scheduler in UNIX/Linux Systems, which executes jobs (scripts) which are scheduled by system, root, or individual users. Information of schedules is contained within crontab file (which is different and individual for each user). When the cron daemon starts, it searches for crontab files, which are user named account names in /var/cron/tabs directory. Cron daemon wakes up every minute and checks for any jobs scheduled to be executed in crontab files. Every time cron daemon wakes up to check crontab files, it also checks /etc/crontab file for any modifications in time stamp of the file, which it keeps track and records. When you simply 'touch' /etc/crontab file, cron daemon will catch it and ultimately stop executing scheduled jobs in crontabs - this is how to stop cron daemon. In order to have cron daemon continue running scheduled jobs, you need to restart it (bounce it).

Controlling access with 'cron.allow' and 'cron.deny'[edit]

  • Access will be granted when user is listed in file /etc/cron.allow
  • Otherwise, access will be denied if username is found in /etc/cron.deny
  • If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to have access, or all users will have access.
  • For standard Debian systems (including Ubuntu), all users will be granted access when neither file exists.
  • For Fedora systems, only the super user will be granted access when neither file exists.


Four places to submit a cron job[edit]

  1. The /var/spool/cron/username file: each individual user (indicated by username) controls his or her own separate file
  2. The /etc/crontab file: This is system crontab file. Only root has permission to modify this file.
  3. The /etc/cron.d directory. Each file placed in this directory has the same format as the /etc/crontab. Only root has permission to create or modify the files in this directory.
  4. The /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories
    • Each file in these directories is a shell script that runs at the times specified in the /etc/crontab file (by default at one minute after the hour, at 4:02 a.m. every day, Sunday at 4:22 a.m., and 4:42 a.m. on the first day of the month, respectively )
    • Only root has permission to create or modify the files in these directories.

Crontab format[edit]

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- day of week (0 - 6) (Sunday=0)
| | | ------- month (1 - 12)
| | --------- day of month (1 - 31)
| ----------- hour (0 - 23)
 ------------ min (0 - 59)
  1. An asterisk (*) in any field indicates all possible values for that field.
  2. Lists of values, ranges of values, and increments can be used. Examples:
    • Lists: Days could be represented as the list Mon,Wed,Fri.
    • Range: Hours can be specified as range 9–5
    • Increment: 0–31/3in the third field to represent every third day of the month
    • */5 denotes every five minutes
  3. Any variable can be specified to fine−tune the environment in which the job will run. Example:
    • MAILTO=otheruser variable enable to send email to user other than the one who submitted the cron job

Commands[edit]

  1. crontab −l : list the current contents of your own personal crontab file
  2. crontab −r : remove all crontab entries
  3. crontab −e : edit crontab file
  4. ps -e |grep cron : Displays status of cron daemon if running.
  5. /usr/sbin/cron & : Starts users cron daemon.