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)

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:

/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:

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

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

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}

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: