SDB:Cron

Jump to: navigation, search
With cron tabs user could schedule own time-based jobs.

Where and how to setup cron jobs

Missing upstream of vixie-cron (our historical default cron) caused a lot of confusion, so we are looking for new cron to replace it. Check these pages: why and how we should switch to cronie.

Version: 11.4+ This text cover properties and features of cronie 1.4.4, which will be available in openSUSE 11.4, so it could differ from the use of openSUSE 11.3 default cron daemon (vixie-cron 4.1). Check Differences between old cron and cronie for more information.


There are several places and ways to schedule your own cron job. There are system crontabs like /etc/crontab and /etc/cron.d/ directory which could use only root (but could define jobs also for users) and the user's crontab which are available through the command crontab -e also for normal user. With all mentioned cases you have to use crontab definitions, see fields. You don't need to use crontab definition if you fit to hourly,minutes,weekly or monthly period.

crontab fields

Meanings of each field in line cover description above (it could contain also more options and field = read whole article)

minute hour day of month month day of week command to be executed
* * * * * command to be executed

Well the meanings of the simplest cron job definition isn't so simple :) (command will be executed each first minute of every hour 00:01, 01:01, 02:01, 03:01 ... etc)

1 * * * *  /path/to/command

More interesting example used slash '/' character to define steps (execute command every 10 minutes)

*/10 * * * *  /path/to/command

In crontabs you could also use ranges using the '-' character (execute every weekdays at 7:30)

30 7 * * 1-5 echo "Wake up, it's morning, go to work!" | sendmail username

Also lists with sets of numbers are allowed (execute command on the first and fifteenth)

* * 1,15 * *  /path/to/command

For the day of the week you could also use: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") and for the month: "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec".

Execute every Thursday at 15:00

0 15 * * * Thu echo "Hey! You have team meeting" | sendmail username

Execute on the first and fifteenth AND every Sunday

* * 1,15 * Sun /path/to/command

If the uid of the crontab owner is 0 (root), he could also use special option to prevent cron from writing a syslog message about the executed command; if you want to use this option use '-' as a first character of crontab entry:

- 1 * * * * username /usr/bin/test

crontab -e command

This is the most common way for users to define a cron job. If you want to edit a cron job just use crontab -e command. It will open the preferred (export EDITOR=vim) editor with already defined cronjobs. Each line represents one cron job definition. With the crontab command you could also list your user cronjobs crontab -l.
Crontab definitions are placed in /var/spool/cron/tabs/
If you check the user crontab file by hand you could see that we have still zombies in our user crontabs:

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXR637JH installed on Tue Apr  6 17:46:35 2010)
# (Cronie version 1.4.4)

/etc/cron.d

This is the place to create a cron job rule for your package and you want an exact date/time of execution. Your crontab rule could be normal file, symlink or hardlink. It has to be owned by root (also write allowed only for root) and not executable.
After default openSUSE installation you could see crontab which are owned by some packages:

ll /etc/cron.d/
total 8
-rw-r--r-- 1 root root  63 2009-10-13 19:44 novell.com-suse_register
-rw-r--r-- 1 root root 268 2009-09-12 00:19 smolt

Cron job rules in syscrondir are not owned by cron package itself, e.g.:

# rpm -qf /etc/cron.d/smolt
smolt-1.4-2.6.1.noarch

As you could see in next listing, files in syscrondir contain standard cronjob rules; with each line we have to specify a user (in our case smolt)

# cat /etc/cron.d/smolt
# Runs the smolt checkin client
# Please note that calling with -c will cause smolt to pause a random amount of
# time between 0 and 3 days before actually sending, this is to prevent ddos on
# the server
20 1 1 * * smolt /usr/bin/smoltSendProfile -c > /dev/null 2>&1

/etc/crontab

In most cases the user doesn't need to change this file. After installation the system cron tab contains one important rule which starts the run-crons script (this script executes all scripts in directories daily, hourly, weekly).

# cat /etc/crontab
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=root
#
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
#
-*/15 * * * *   root  test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1

cron.hourly cron.daily cron.weekly cron.monthly

This is the good place when you want create cron job rule for your package and you don't need to use crontab rule for exact time. These directories are the simple way to schedule executing scripts in daily, hourly, weekly or monthly period. You have to just push your script (owned by root, with exec permissions) to one of these directories. See listing of daily cronjobs, again all crontab files are not owned by cron package

#> ll /etc/cron.daily/
total 32
-rwxr-xr-x 1 root root  587 2009-10-24 05:20 logrotate
-rwxr--r-- 1 root root  948 2009-10-24 05:50 suse-clean_catman
-rwxr-xr-x 1 root root 1875 2003-09-01 13:10 suse.de-backup-rc.config
-rwxr-xr-x 1 root root 2059 2003-09-08 15:50 suse.de-backup-rpmdb
-rwxr-xr-x 1 root root  566 2004-07-23 12:38 suse.de-check-battery
-rwxr-xr-x 1 root root 1314 2005-07-27 15:28 suse.de-clean-tmp
-rwxr-xr-x 1 root root  371 2003-09-01 13:10 suse.de-cron-local
-rwxr--r-- 1 root root 1693 2009-10-24 05:50 suse-do_mandb

As mentioned above, processing of these directories is provided by the shell script /usr/lib/cron/run-crons. This script is triggered by cron job (defined in /etc/crontab) every 15 minutes. If you have build service account you could check run-crons script here. Basically it execute /etc/sysconfig/cron (inside aaa_base package) to get sysconfig settings, evaluate this settings and execute scripts in mentioned directories in appropriate time period.

Parts of cron spread in the file system

/etc/cron.deny Users listed in this file can't use crontab -e command to add cron job rule, but their Cron jobs will be executed (if already defined)
/etc/cron.allow If this file exists and you want to use crontab -e command, your user has to be added to this file
/etc/crontab main crontab
/etc/init.d/cron cron init script
/etc/sysconfig/cron sysconfig of initscript which is processed by run-crons script. Using this config you could change the behavior of processing of scripts which are placed inside /etc/cron.{hourly,daily,weekly,monhtly}
/etc/omc/srvinfo.d/cron.xml see omc umbrella project http://developer.novell.com/wiki/index.php/OMC
/usr/bin/crontab command intended for editing user crontab (setuid bit set)
/usr/lib/cron/run-crons this script is executed every 15 minutes from main crontab and processes scripts placed in /etc/cron.{hourly,daily,weekly,monhtly}
/usr/sbin/cron cron daemon binary executed from init script
/usr/sbin/rccron just a link to /etc/init.d/cron
/var/spool/cron/lastrun this directory could contain files cron.hourly,cron.daily, cron.weekly, cron.monthly which marks last execution time of scripts from /etc/cron.{hourly,daily,weekly,monthly}
/var/spool/cron/tabs user crontabs -- read and write access only for root
/var/run/cron.pid pid of currently runnig cron daemon

Existing jobs

cron.hourly

suse.de-snapper: creates hourly snapshots if TIMELINE_CREATE="yes" in a config file in /etc/snapper/configs/

cron.daily

mdadm : Runs mdadm --monitor --oneshot once a day to ensure degraded arrays don't go unnoticed.

packagekit-background.cron : (off by default) Updates with pkon, or checks for updates, depending on settings in /etc/sysconfig/packagekit-background

suse-clean_catman : Deletes old preformatted man-pages every 7 days, based on settings in /etc/sysconfig/cron

suse-do_mandb : Runs mandb, based on settings in /etc/sysconfig/cron

suse.de-backup-rc.config : Creates backup archive of sysconfig files, based on settings in /etc/sysconfig/backup

suse.de-backup-rpmdb : Creates backups of the RPM database, based on settings in /etc/sysconfig/backup

suse.de-check-battery : Checks the CMOS battery.

suse.de-cron-local : ???

suse.de-snapper : Runs the snapper cleanup algorithms (number, timeline, empty-pre-post), based on settings in /etc/sysconfig/snapper

cron.weekly

btrfs-balance : Runs btrfs balance, based on settings in /etc/sysconfig/btrfsmaintenance

btrfs-trim : Runs fstrim, based on settings in /etc/sysconfig/btrfsmaintenance

cron.monthly

btrfs-scrub : Runs btrfs scrub, based on settings in /etc/sysconfig/btrfsmaintenance

Cron daemon options

debug cron

If cron doesn't do what you wish, use debug options: stop the cron daemon and start cron daemon with -x option with appended one or more debug flags. You could also use -n option to run cron daemon in foreground:

# rccron stop
# cron -nx pars,load

Available debug flags : ext,sch,proc,pars,load,misc,test,bit

Internal links

External links