1.计划任务基本概念
linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作用的,所以crond才符合我们的要求。
crontab支持两种状态:一、直接编写计划任务;二、使用
目录的方式,放在目录里面的都会定时执行。
crond守护进程每分钟检查一次相关配置,在配置做了修改之后不需要重新启动该进程即可生效;
cron执行的每一项工作都会被纪录到
/var/log/cron这个日志文件中,可以从这个文件查看命令执行的状态。
chkconfig --list crond
service crond status
man 8
crond
NAME
cron - daemon to execute scheduled commands (ISC Cron V4.1)
SYNOPSIS
cron [-l load_avg] [-n] [-p] [-m<mailcommand>]
DESCRIPTION
Cron should be started from /etc/rc.d/rc or /etc/rc.d/rc.local. It
will return immediately, so you don’t need to start it with ’&’. The
-n option changes this default behavior causing it to run in the fore-
ground. This can be useful when starting it out of init.
Cron searches /var/spool/cron for crontab files which are named after
accounts in /etc/passwd; crontabs found are loaded into memory. Cron
also searches for /etc/crontab and the files in the /etc/cron.d direc-
tory, which are in a different format (see crontab(5)). Cron then
wakes up
every minute, examining all stored crontabs, checking each
command to see if it should be run in the current minute. When execut-
ing commands, any output is mailed to the owner of the crontab (or to
the user named in the MAILTO environment variable in the crontab, if
such exists).
Additionally,
cron checks each minute to see if its spool directory’s
modtime (or the modtime on /etc/crontab) has changed, and if it has,
cron will then examine the modtime on all crontabs and reload those
which have changed. Thus
cron need not be restarted whenever a crontab
file is modified. Note that the Crontab(1) command updates the modtime
of the spool directory whenever it changes a crontab.
The -m option allows you to specify a shell command string to use for
sending cron mail output instead of sendmail(8). This command must
accept a fully formatted mail message (with headers) on stdin and send
it as a mail message to the recipients specified in the mail headers.
Daylight Saving Time and other time changes
Local time changes of less than three hours, such as those caused by
the start or end of Daylight Saving Time, are handled specially. This
only applies to jobs that run at a specific time and jobs that are run
with a granularity greater than one hour. Jobs that run more fre-
quently are scheduled normally.
If time has moved forward, those jobs that would have run in the inter-
val that has been skipped will be run immediately. Conversely, if time
has moved backward, care is taken to avoid running jobs twice.
Time changes of more than 3 hours are considered to be corrections to
the clock or timezone, and the new time is used immediately.
PAM Access Control
On Red Hat systems, crond now supports access control with PAM - see
pam(8). A PAM configuration file for crond is installed in
/etc/pam.d/crond. crond loads the PAM environment from the pam_env
module, but these can be overriden by settings in the crontab file.
SIGNALS
On receipt of a SIGHUP, the cron daemon will close and reopen its log
file. This is useful in scripts which rotate and age log files. Natu-
rally this is not relevant if cron was built to use syslog(3).
CAVEATS
In this version of cron , without the -p option, /etc/crontab must not
be writable by any user other than root, no crontab files may be links,
or linked to by any other file, and no crontab files may be executable,
or be writable by any user other than their owner.
SEE ALSO
crontab(1), crontab(5), pam(8)
2.系统crontab文件;man 5 crontab
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
(分 时 日 月 周) 用户 run-parts 单目录参数
如果需要增加系统的计划任务,只需要在对应的目录添加执行文件即可,例如:我需要在每天都执行updatedb的操作,则我只需要把/usr/bin/updatedb的执行命令链接到/etc/cron.daily目录就可以了。
同样的,如果不需要使用目录的方式,也可以使用如下的方式:
02 01 * * * root /root/test.sh
也就是没有了run-parts,后面就直接跟命令的绝对路径
3.run-parts命令(脚本); cat /usr/bin/run-parts
#!/bin/bash
# run-parts - concept taken from Debian
# keep going when something fails
set +e
if [ $# -lt 1 ]; then
echo "Usage: run-parts <dir>"
exit 1
fi
if [ ! -d $1 ]; then
echo "Not a directory: $1"
exit 1
fi
# Ignore *~ and *, scripts
for i in $1/*[^~,] ; do
[ -d $i ] && continue
# Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
[ "${i%.rpmsave}" != "${i}" ] && continue
[ "${i%.rpmorig}" != "${i}" ] && continue
[ "${i%.rpmnew}" != "${i}" ] && continue
[ "${i%.swp}" != "${i}" ] && continue
[ "${i%,v}" != "${i}" ] && continue
if [ -x $i ]; then
$i 2>&1 | awk -v "progname=$i" \
'progname {
print progname ":\n"
progname="";
}
{ print; }'
fi
done
exit 0
4.目录/etc/cron.*ly里是希望执行的任务脚本
5.用户crontab文件和crontab命令
在系统注册的用户(/etc/passwd)
ls -l /usr/spool/
可以使用crontab命令编辑用户的计划任务
命令:
crontab [-u username] [-l|-e|-r]
参数:
-u :通过-u帮其它使用者建立/移除 crontab;
-e :编辑 crontab 的内容
-l :查看 crontab 的内容
-r :移除 crontab 的所有内容(是全部的内容,如果只是删除某个,用-e编辑即可)
内容格式:
* * * * * 命令
前面的五个*号,表示分、时、日、月、周,如:
代表意义 分钟 小时 日期 月份 周
数字范围 0-59 0-23 1-31 1-12 0-7
*号代表任何时间都接受的意思,任意。
*号之间用空格分开,如果是一段范围,用-号连接;如果是隔开几个时间,用,号表示。
另外,命令必须是编写计划任务的用户有权限执行的,并且最后用绝对路径。
例如:
#crontab -e
59 23 1 5 * mail linuxing < /home/test.txt
每在5月1日,23点59分就把/home/test.txt的内容作为邮件发给linuxing用户
*/5 * * * * /opt/test.sh
每5分钟就执行一次/opt/test.sh脚本
0 3,6 * * * /usr/local/bin/test.sh
每在3点和6点整点都执行/usr/local/bin/test.sh命令
0 8-12 * * * /root/backup.sh
8 点到 12 点之间的每小时的0分都执行/root/backup.sh
6./etc/cron.d目录,用来简化软件包管理
该目录通常是空的
7.日志 /var/log/cron
cron执行的每一项工作都会被纪录到/var/log/cron这个日志文件中,可以从这个文件查看命令执行的状态
cat /var/log/cron
Nov 2 03:56:02 server1 anacron[3024]: Job `cron.daily' terminated
Nov 2 03:56:02 server1 anacron[3024]: Normal exit (1 jobs run)
Nov 2 04:01:01 server1 crond[6721]: (root) CMD (run-parts /etc/cron.hourly)
Nov 2 04:02:01 server1 crond[6751]: (root) CMD (run-parts /etc/cron.daily)
Nov 2 04:02:01 server1 anacron[6754]: Updated timestamp for job `cron.daily' to 2
009-11-02
Nov 3 03:26:57 server1 crond[3195]: (CRON) STARTUP (V5.0)
Nov 3 03:26:58 server1 anacron[3249]: Anacron 2.3 started on 2009-11-03
Nov 3 03:26:58 server1 anacron[3249]: Will run job `cron.daily' in 65 min.
Nov 3 03:26:58 server1 anacron[3249]: Jobs will be executed sequentially
Nov 3 04:08:12 server1 crond[4679]: (root) CMD (run-parts /etc/cron.hourly)
Nov 3 04:09:18 server1 crond[5253]: (root) CMD (run-parts /etc/cron.daily)
Nov 3 04:16:14 server1 crond[3219]: (CRON) STARTUP (V5.0)
Nov 3 04:16:15 server1 anacron[3273]: Anacron 2.3 started on 2009-11-03
Nov 3 04:16:15 server1 anacron[3273]: Will run job `cron.daily' in 65 min.
Nov 3 04:16:15 server1 anacron[3273]: Jobs will be executed sequentially
Nov 3 04:41:24 server1 crond[3220]: (CRON) STARTUP (V5.0)
Nov 3 04:41:25 server1 anacron[3274]: Anacron 2.3 started on 2009-11-03
Nov 3 04:41:26 server1 anacron[3274]: Will run job `cron.daily' in 65 min.
Nov 3 04:41:26 server1 anacron[3274]: Jobs will be executed sequentially
Nov 3 05:01:01 server1 crond[4066]: (root) CMD (run-parts /etc/cron.hourly)
Nov 3 05:29:08 server1 anacron[3274]: Received SIGUSR1
Nov 3 05:29:08 server1 anacron[3274]: Exited
Nov 4 05:09:55 server1 crond[3191]: (CRON) STARTUP (V5.0)
Nov 4 05:09:56 server1 anacron[3245]: Anacron 2.3 started on 2009-11-04
Nov 4 05:09:56 server1 anacron[3245]: Will run job `cron.daily' in 65 min.
Nov 4 05:09:56 server1 anacron[3245]: Jobs will be executed sequentially
Nov 4 05:36:40 server1 anacron[3245]: Received SIGUSR1
Nov 4 05:36:40 server1 anacron[3245]: Exited
Nov 4 23:35:05 server1 crond[3197]: (CRON) STARTUP (V5.0)
Nov 4 23:35:06 server1 anacron[3251]: Anacron 2.3 started on 2009-11-04
Nov 4 23:35:06 server1 anacron[3251]: Will run job `cron.daily' in 65 min.
Nov 4 23:35:06 server1 anacron[3251]: Jobs will be executed sequentially
Nov 5 00:01:01 server1 crond[4252]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 00:40:06 server1 anacron[3251]: Job `cron.daily' started
Nov 5 00:40:31 server1 anacron[3251]: Job `cron.daily' terminated (mailing output
)
Nov 5 00:40:31 server1 anacron[3251]: Normal exit (1 jobs run)
Nov 5 01:01:01 server1 crond[6538]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 02:01:01 server1 crond[8211]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 03:01:01 server1 crond[9858]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 04:01:01 server1 crond[11518]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 04:02:01 server1 crond[11547]: (root) CMD (run-parts /etc/cron.daily)
Nov 5 04:02:01 server1 anacron[11550]: Updated timestamp for job `cron.daily' to
2009-11-05
Nov 5 05:01:01 server1 crond[13694]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 06:01:01 server1 crond[15368]: (root) CMD (run-parts /etc/cron.hourly)
Nov 5 06:52:23 server1 anacron[16892]: Anacron 2.3 started on 2009-11-05
Nov 5 06:52:23 server1 anacron[16892]: Normal exit (0 jobs run)
Nov 5 07:01:01 server1 crond[17963]: (root) CMD (run-parts /etc/cron.hourly)
Nov 7 01:46:34 server1 crond[3207]: (CRON) STARTUP (V5.0)
Nov 7 01:46:36 server1 anacron[3261]: Anacron 2.3 started on 2009-11-07
Nov 7 01:46:36 server1 anacron[3261]: Will run job `cron.daily' in 65 min.
Nov 7 01:46:36 server1 anacron[3261]: Will run job `cron.weekly' in 70 min.
Nov 7 01:46:36 server1 anacron[3261]: Jobs will be executed sequentially
Nov 7 02:01:01 server1 crond[3878]: (root) CMD (run-parts /etc/cron.hourly)
Nov 7 02:51:36 server1 anacron[3261]: Job `cron.daily' started
Nov 7 02:52:55 server1 anacron[3261]: Job `cron.daily' terminated (mailing output
)
Nov 7 02:56:36 server1 anacron[3261]: Job `cron.weekly' started
Nov 7 02:59:30 server1 anacron[3261]: Job `cron.weekly' terminated
Nov 7 02:59:30 server1 anacron[3261]: Normal exit (2 jobs run)
Nov 7 03:01:01 server1 crond[6892]: (root) CMD (run-parts /etc/cron.hourly)
Nov 7 04:01:01 server1 crond[8665]: (root) CMD (run-parts /etc/cron.hourly)
Nov 7 04:02:01 server1 crond[8695]: (root) CMD (run-parts /etc/cron.daily)
Nov 7 04:02:01 server1 anacron[8698]: Updated timestamp for job `cron.daily' to 2
009-11-07
Nov 7 05:01:01 server1 crond[10600]: (root) CMD (run-parts /etc/cron.hourly)
Nov 7 05:03:23 server1 crontab[10703]: (root) LIST (root)
Nov 7 05:03:28 server1 crontab[10704]: (root) BEGIN EDIT (root)
Nov 7 05:03:35 server1 crontab[10706]: (root) LIST (root)
Nov 7 06:01:00 server1 anacron[12449]: Anacron 2.3 started on 2009-11-07
Nov 7 06:01:00 server1 anacron[12449]: Normal exit (0 jobs run)
Nov 7 06:01:01 server1 crond[12510]: (root) CMD (run-parts /etc/cron.hourly)
[root@server1 log]#