Cron

De WikiMar
Dreceres ràpides: navegació, cerca

Crontab format

crontab -e

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

# m h dom mon dow user	command

What executes /etc/cron.daily weekly monthly hourly ?

  • crontab is running on memory
  • anacron is run by periodically by crontab
  • /etc/cron.hourly is run by crontab (anacron only has precision of days)

crontab runs cron.hourly which runs anacron which has /etc/anacron which run:

  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly


In a new Ubuntu you can see in /etc/crontab (not visible for sudo crontab -e):

# 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 )
#

In Fedora there is a at /etc/cron.d/0hourly

# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

and in /etc/cron.d/dailyjobs

# Run the daily, weekly, and monthly jobs if cronie-anacron is not installed
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# run-parts
02 4 * * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.daily
22 4 * * 0 root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.weekly
42 4 1 * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.monthly


Cron vs Anacron

Cron Anacron
Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute) Minimum granularity is only in days
Cron job can be scheduled by any normal user ( if not restricted by super user ) Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )
Cron expects system to be running 24 x 7. If a job is scheduled, and system is down during that time, job is not executed. Anacron doesn’t expect system to be running 24 x 7. If a job is scheduled, and system is down during that time, it start the jobs when the system comes back up.
Ideal for servers Ideal for desktops and laptops
Use cron when a job has to be executed at a particular hour and minute Use anacron when a job has to be executed irrespective of hour and minute

More info: http://www.thegeekstuff.com/2011/05/anacron-examples/