系统任务调度
配置文件:/etc/crontab
[root@VM_170_59_centos etc]# cat crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
# 前 三行是用来配置crond任务运行的环境变量,
# 第一行SHELL变量指定了系统要使用哪个shell,这里是bash,
# 第二行PATH变量指定了系统执行 命令的路径,
# 第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户
用户任务调度
安装crontab
[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs
说明:
vixie-cron软件包是cron的主程序;
crontabs软件包是用来安装、卸装、或列举用来驱动cron守护进程的表格的程序。
cron 是linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:
/sbin/service crond start #启动服务
/sbin/service crond stop #关闭服务
/sbin/service crond restart #重启服务
/sbin/service crond reload #重新载入配置
查看crontab服务状态:
service crond status
手动启动crontab服务:
service crond start
其他命令:
# 查看crontab服务是否已设置为开机启动,执行命令:
ntsysv
# 加入开机自动启动:
chkconfig --level 35 crond on
# 列出crontab文件
crontab -l
# 编辑crontab文件
crontab -e
# 删除crontab文件
$ crontab -r
# 恢复丢失的crontab文件
# 假设你在自己的$HOME目录下还有一个备份,那么可以将其拷贝到/var/spool/cron/<username>,其中<username >是用户名
# 或者使用如下命令其中,<filename>是你在$HOME目录中副本的文件名
crontab <filename>
日志文件:/var/log/cron*
==补充:==
1、crontab相关命令
功能说明:设置计时器。
语 法:crontab [-u <用户名称>][配置文件] 或 crontab [-u <用户名称>][-elr]
补充说明:cron是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使用计时器的功能。
配置文件格式:Minute Hour Day Month DayOFWeek Command
参 数:
-e 编辑该用户的计时器设置。
-l 列出该用户的计时器设置。
-r 删除该用户的计时器设置。
-u<用户名称> 指定要设定计时器的用户名称。
2、crontab 配置文件格式
基本格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
#每晚的21:30 重启apache
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#每月1、10、22日的4 : 45重启apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#每周六、周日的1 : 10重启apache
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
#每天18 : 00至23 : 00之间每隔30分钟重启apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
#每星期六的11 : 00 pm重启apache
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间,每隔一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
# 每一小时重启apache
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
# 每月的4号与每周一到周三的11点重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
# 一月一号的4点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
# 每半小时同步一下时间
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44
3、其他任务调度
# cron默认配置了调度任务,分别为:hourly、daily、weekly、mouthly,默认配置文件为/etc/anacrontab
# 将需要执行的脚本放到相应的目录下即可,目录分别为:
/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/ect/cron.mouthly
网友评论