Centos设置定时脚本

Centos设置定时脚本

1. 安装crontab

首先检查是否安装crontab,输入命令

crontab -l

若没有提示命令不存在则说明已安装,可以跳过安装步骤
Centos下安装crontab的命令为

yum install vixie-cron
yum install crontabs

vixie-cron软件包是cron的主程序;

crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。

2. 配置定时脚本

配置说明:

# 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

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

这里举几个例子,基本涵盖了常见的一些情况:

实例1

5   *    *      *   *   ls     指定每小时的第5分钟执行一次ls命令
30   5    *      *   *   ls     指定每天的 5:30 执行ls命令
30   7    8      *   *   ls     指定每月8号的7:30分执行ls命令
30   5    8      6   *   ls     指定每年的6月8日5:30执行ls命令
30   5    8      6   *   ls     指定每年的6月8日5:30执行ls命令
30   6    *      *   0   ls     指定每星期日的6:30执行ls命令
30   3   10,20     *   *   ls     每月10号及20号的3:30执行ls命令
25   8-11  *      *   *   ls     每天8-11点的第25分钟执行ls命令
*/15  *    *      *   *   ls     每15分钟执行一次ls命令
30   6   */10     *   *   ls     每个月中,每隔10天6:30执行一次ls命令
22   4    *      *   *   root   run-parts   /etc/cron.daily
#每天4:22以root身份执行/etc/cron.daily目录中的所有可执行文件,run-parts参数表示,执行后面目录中的所有可执行文件。

实例2

#每晚的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
0 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小时重启apache
0 */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
#每半小时同步一下时间
0/30 * * * * /usr/sbin/ntpdate 210.72.145.44
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值