cron定时任务
Crontab 定时任务
var/spool/cron 目录下存放的是每个用户包括root的crontab任务,每个任务以创建者的名字命名
/etc/crontab 这个文件负责调度各种管理和维护任务。
etc/cron.d 这个目录用来存放任何要执行的crontab文件或脚本。
我们还可以把脚本放在/etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly、/etc/cron.monthly目录中,让它每小时/天/星期、月执行一次。
crontab文件内容
root@egw602:~# cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
# 1 * * * * root cd / && run-parts /etc/cron.hourly
# 30 7 * * * root cd / && run-parts /etc/cron.daily
# 42 7 * * 7 root cd / && run-parts /etc/cron.weekly
# 55 7 1 * * root cd / && run-parts /etc/cron.monthly
cron前面5个时间参数的说明
minute(0 ~ 59), hour(0 ~ 23), day of month(1 ~ 31), month(1 ~ 12), day of week(0 ~ 6, sunday = 0 or 7)
特殊符号的含义
*
代表任何时间,比如第一个 * 就代表一小时中的每分钟都执行,
代表不连续的时间,比如 0 8,12,16 * * * 代表每天8,12,16点0分执行-
代表连续的时间范围,比如0 5 * * 1-6 代表在周一到周六凌晨5点0分执行*/n
代表每个多久执行一次,比如*/10
代表每隔10分钟执行一次
使用例子
每1分钟执行一次myCommand
* * * * * myCommand
每小时的第3和第15分钟执行
3,15 * * * * myCommand
在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand
每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * myCommand
每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 myCommand
每晚的21:30重启smb
30 21 * * * /etc/init.d/smb restart
每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart
每周六、周日的1 : 10重启smb
10 1 * * 6,0 /etc/init.d/smb restart
每天18 : 00至23 : 00之间每隔30分钟重启smb
0,30 18-23 * * * /etc/init.d/smb restart
每星期六的晚上11 : 00 pm重启smb
0 23 * * 6 /etc/init.d/smb restart
每一小时重启smb
0 */1 * * * /etc/init.d/smb restart
晚上11点到早上7点之间,每隔一小时重启smb
0 23-7/1 * * * /etc/init.d/smb restart
最后编辑:SteveChen 更新时间:2025-08-17 21:38