1.简单介绍
• 软件包:cronie、crontabs
• 系统服务:crond
• 日志文件:/var/log/crond 排除故障
2.命令:
crontab -e 创建任务
-u 以那个用户创建
-r 删除任务
-l 查看
3.计划任务格式(图片转载自网络)
周期性计划任务内容格式:
分 时 日 月 周 (时间从分到周排列,最小时间单位是分,*代表任意时间,五个*代表每月每周每日每分中进行一次,每个时间之间用一个空格隔开)
* * * * * +要计划执行的任务 注意要 执行的任务命令尽量使用绝对路径
#crontab -e -u root
* * * * * /usr/bin/echo hello linux >> /opt/linux.txt
crontab: installing new crontab
[root@server0 ~]#
4.检查执行情况
[root@server0 ~]# cat /opt/linux.txt
hello linux
[root@server0 ~]# cat /opt/linux.txt
hello linux
hello linux
hello linux
hello linux
[root@server0 ~]# cat /opt/linux.txt
hello linux
hello linux
hello linux
hello linux
hello linux
hello linux
[root@server0 ~]#
5.全局主配置文件 /etc/crontab 主要针对系统 用户配置文件/var/spool/cron/* 主要是用户创建的计划任务文件
配置好文件,最好检查服务商否运行 用pgrep crond 有进程则说明服务在运行
另最好将服务设为开机自启动 systemctl enable crond
6.实用举例(从其他地方复制的)
实例1:每1分钟执行一次command
命令:* * * * * command
实例2:每小时的第3和第15分钟执行
命令:3,15 * * * * command
实例3:在上午8点到11点的第3和第15分钟执行
命令:3,15 8-11 * * * command
实例4:每隔两天的上午8点到11点的第3和第15分钟执行
命令:3,15 8-11 */2 * * command
实例5:每个星期一的上午8点到11点的第3和第15分钟执行
命令:3,15 8-11 * * 1 command
实例6:每晚的21:30重启smb
命令:30 21 * * * /etc/init.d/smb restart
实例7:每月1、10、22日的4 : 45重启smb
命令:45 4 1,10,22 * * /etc/init.d/smb restart
实例8:每周六、周日的1 : 10重启smb
命令:10 1 * * 6,0 /etc/init.d/smb restart
实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb
命令:0,30 18-23 * * * /etc/init.d/smb restart
实例10:每星期六的晚上11 : 00 pm重启smb
命令:0 23 * * 6 /etc/init.d/smb restart
实例11:每一小时重启smb
命令:* */1 * * * /etc/init.d/smb restart
实例12:晚上11点到早上7点之间,每隔一小时重启smb
命令:* 23-7/1 * * * /etc/init.d/smb restart
实例13:每月的4号与每周一到周三的11点重启smb
命令:0 11 4 * mon-wed /etc/init.d/smb restart
实例14:一月一号的4点重启smb
命令:0 4 1 jan * /etc/init.d/smb restart
实例15:每小时执行/etc/cron.hourly目录内的脚本
命令:01 * * * * root run-parts /etc/cron.hourly
说明:
run-parts这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是目录名了