凌的博客

您现在的位置是: 首页 > 学无止境 > Linux > 

Linux

Centos 定时任务crond

2015-03-18 Linux 930
Centos系统一般自带安装了crond工具,但也不排除有例外的情况,因此,大家需要在进行备份之前需要确认本机已经安装了crond工具。命令:service crond status,查看进程的运行状态,如果没有系统没有找不出此服务,
Centos系统一般自带安装了crond工具,但也不排除有例外的情况,因此,大家需要在进行备份之前需要确认本机已经安装了crond工具。命令:service crond status,查看进程的运行状态,如果没有系统没有找不出此服务,则表示系统并没有安装此进程。
第一步:如果没有安装,请用下面命令安装crond
yum install -y vixie-cron
service crond start
并设置crond自启动,在/etc/rc.d/rc.local中,在末尾加上service crond start。
第二步:编辑/etc/crontab文件
在末尾加上一行: 30 5 * * * root init 6 //系统配置为了每天早上5点30自动重新启动。
第三步:crontab一些相关命令
/sbin/service crond restart //重启cron服务
crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数(fg:crontab -u root)
crontab -l //列出某个用户cron服务的详细内容(fg:crontab -u root -l)
crontab -r //删除某个用户的cron服务(fg:crontab -u root -r)
crontab -e //编辑某个用户的cron服务(fg:crontab -u root -e)
crontab -d //删除当前的crontab任务
crontab filename //以filename做为crontab的任务列表文件并载入
第四步:crontab file的格式
    crontab 的命令行由 6 个字段组成,不同字段间用空格或 tab 键分隔。每条命令如果不需要使用,必须用*代替。
命令写法:{分钟 (0-59)}{小时 (0-23)}{日期 (1-31)}{月份 (1-12)}{星期几(0-6,其中 0 代表星期日)} {要执行的字符串}
常用命令分享:
      10 6 * * * reboot //每天早上6点10分
      0 */2 * * * reboot //每两个小时
      0 23-7/2,8 * * * reboot //晚上11点到早上8点之间每两个小时,早上8点
      0 15 10 * mon-wed reboot //每个月的10号和每个星期的周一到周三的早上15点
      0 4 1 jan * date //1月份日早上4点

文章评论

0条评论