SDB:Cron

跳转至: 导航, 搜索
使用 cron tab 用户可以计划自己基于时间的任务。

在哪里以及如何设置 cron 任务

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

版本:11.4+ This text cover properties and features of new cronie 1.4.4, which will be available in openSUSE 11.4, so could differ in case of 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 how to schedule own cron job, basically 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 users 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 of course use also ranges using '-' 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

As day of week you could use also short name instead of digit ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") similar for 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

Example bellow will run 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 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 how to user define his cron job. If you want edit cron job just use crontab -e command. It will open preferred (export EDITOR=vim) editor with already defined cronjobs. Each line is represent one cron job definition. With crontab command you could also list your user cronjobs crontab -l.
Crontab definitions are placed in /var/spool/cron/tabs/
If you check 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 good place when you want to create cron job rule for your package and you want exact date/time of execution. Your crontab rule could be normal file,symlink or hardlink. It have 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, whit ine exception we have to specify 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 case user doesn't need to change this file. After installation system cron tab contain one important rule which start run-crons script (this script execute 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 simple way, how to schedule script executing 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 I mention above, processing of these directories provides shell script /usr/lib/cron/run-crons. This script is triggered by cron job (defined in syscrontab) 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 are already defined)
/etc/cron.allow If this file exist and you want to use crontab -e command, your user have 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 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 process scripts places 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,monhtly}
/var/spool/cron/tabs in this directory are placed user crontabs read and write access only for root
/var/run/cron.pid pid of currently runnig cron daemon

Cron 守护精灵选项

调试 cron

若 cron 没有如您所愿运作,您可使用调试选项:停止 cron 守护精灵并使用 -x 选项启动 cron 守护精灵,可附加一个或多个调试标记。您也可以使用 -n 选项来在前台运行 cron 守护精灵:

# sudo systemctl stop cron.service
# cron -nx pars,load

可用调试标记:ext,sch,proc,pars,load,misc,test,bit