计划任务(定时任务)
文件位置:
/etc/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
22 * * * * * root touch b.txt 每天的每小时的第22分钟做什么事
25 15 * * * root touch /tmp/c.txt 每天下午的三点25分
27 15,16,17 * * * root touch /tmp/d.txt 每天的15点,16点,17点的第27分钟
28 15-19 * * * root touch /tmp/e.txt 表示 每天的15点到19点的28分钟
30 10-20/5 * * * root touch /tmp/f.txt 每天的10点到20点每个5小时
*/7 * * * * root touch /tmp/f.txt 从0开始重新结算
37 * 10,20,30 * 4 echo $(date) >> /tmp/g.txt 表示每个月的10,20,30或者每周的周四
建议:
分钟不要写成*
crontab 命令
-e 编辑
-u 指定用户,如果不写,默认为当前用户
-l 列出当前用户的计划任务
-r 删除用户的计划任务
- 默认写入的文件在/var/spool/cron/username
计划任务作用
- 定时删除
- 定时备份
- 同步时间 ntpdate