cron: 周期性计划任务, 循环性计划任务, 主要用于备份数据.
- crontab
- usage:crontab [-u user] [ -e | -l | -r ]
- -u <user> define user #指定用户
- -e edit user's crontab #编辑计划任务
- -l list user's crontab #查看计划任务
- -r delete user's crontab #删除计划任务
1,crontab -e 编辑周期性计划任务的内容
2, 查看配置: cat /etc/crontab
时间
min hour day month week command
分钟 小时 日 月 周 动作 (任务命令)
0-59 0-23 1-31 1-12 0-7 命令或脚本 (写绝对路径)
- 30 2 1 * * rm -rf /tmp/* // 每月 1 号的 2:30 分删除 / tmp 目录下的所有文件
- 5 * * 6 3 rm -rf /tmp/* // 每年 6 月的每周三每小时的 5 分钟删除 / tmp 目录下的所有文件
- 6 6 * 6 6 rm -rf /tmp/* // 每年 6 月的周六的 6 点 6 分钟删除 / tmp 目录下的所有文件
* 任何时刻
- 一段时间 // 8-11 * * * * rm -rf /tmp/* // 每小时的 8 分钟 9 分钟 10 分钟 11 分钟 删除 / tmp 目录下所有文件
, 分隔 (不连续的时间) // 1,2,5,9 * * * * rm -rf /tmp/* // 每小时的 1 分钟 2 分钟 5 分钟 9 分钟 删除 / tmp 目录下所有文件
0-4,8-12 //
0-23/2 时间间隔 //1 0-23/2 * * * rm -rf /tmp/* // "0,2,4,6,8,10,12,14,16,18,20,22").
*/2 每隔 2 小时 //1 */2 * * * rm -rf /tmp/* 每隔 2 小时执行 rm 命令
* 0-23 * * * rm -rf /tmp/*
* */1 * * * rm -rf /tmp/*
3, 时间的写法
The time and date fields are:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names,
see below)
day of week 0-7 (0 or 7 is
Sun, or use names)
案例一: 每个星期三的下午 10:00 备份 / etc / 目录到 / tmp 目录叫 2017-04-12_etc.tar.gz 备份文件包含当天日期
crontab -e
* 22 * * 3 /usr/bin/tar cvzf /tmp/`/usr/bin/date +%F`_etc.tar.gz /etc
注:` 命令 ` 中的 `` 为命令替换功能, 系统会先执行 ``(反撇, 反引号) 中的命令.也可以将 `` 写成 $()
案例二: 只保留最近 1 个月的日志文件
crontab -e
1 * * * * find /var/log -mtime +30 |xarge -i rm -rf {}
案例三: 每月 1 号的凌晨 2 点半清空临时盘
crontab -e
30 2 1 * * /usr/bin/rm -rf /tmp/*
案例四: 每周每天的晚上 8:10 关闭电脑, 除了星期六, 星期五, 星期日的 17:10 关闭电脑
crontab -e
10 20 * * 1-4 /usr/sbin/poweroff
10 17 * * 5,7 /usr/sbin/poweroff
案例五: jim 用户每天在家目录创建一个以日期命名的目录 20170412
crontab -u jim -e
1 0 * * * /bin/mkdir /home/jim/`date +%Y%m%d`
4, 查询周期性计划任务的内容
crontab -l #查看当前用户
crontab -u jim - #查看指定用户
5, 删除某用户的所有周期性计划任务 (删除某个计划任务 crontab -e 进入文本删除)
crontab -r #当前用户
crontab -u jim -r #指定用户
系统定义的计划任务 (默认)
更新 whatis 数据库
更新 updatedb 数据库
日志定期轮滚 (日志切割)
清理 / tmp /var/tmp
收集系统的状态信息
VIM /etc/crontab 配置文件
# 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
*/5 * * * * root 命令 // 每隔 5 分钟以 root 身份去执行命令
以下目录内可以存在脚本 该脚本就会按照时间去执行
- cd /etc
- cron.hourly/ #每小时
- cron.weekly/ #每周
- cron.daily/ #每天
- cron.monthly/ #每月
6, 删除超过一个月不使用的文件
cat /etc/cron.daily/tmpwatch
7,crontab 服务管理:
查软件是否已安装: rpm -q crontabs
查软件的文件列表: rpm -ql crontabs
查软件的配置文件列表: rpm -qc crontabs
服务状态: systemctl status crond 注: d 是 daemon 守护进程
重启 at 服务: systemctl restart crond 或 service crond restart
关闭 at 服务: systemctl stop crond 或 service crond stop
开机不启动 at 服务: systemctl disable crond 或 chkconfig crond off
开机启动 at 服务: systemctl enable crond 或 chkconfig crond on
8,cron 权限管理
/etc/cron.allow 白名单: 只允许在此名单内的用户使用 crontab(此文件本身不存在)
/etc/cron.deny 黑名单: 在此名单内的用户不可以使用 crontab
注: 两个文件都不存在, 只有 root 用户可以使用 crontab
两个文件都存在, allow 优先执行
---------------------
来源: http://www.bubuko.com/infodetail-3149152.html