计划任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计划任务相关的知识,希望对你有一定的参考价值。
一:一次性的计划任务at 临时的创建计划任务
如:at 23:00
at> shutdown -h now
按Ctrl +d 结束
查看:
atq 或at -l #查看当前的全部计划任务
删除:
atrm 或at -d +计划任务的job号 # 删除计划任务
二:at的时间表示方法
HH:MM //18:30
midnight //0点 (午夜)
noon //12点 (中午)
teatime //16点(下午4点)
AM 或 PM后缀指定一天的上午或下午。 //at 2:00 pm
MMDDYY 、 MM/DD/YY 或 DD.MM.YY
now + 计数 时间单位 的式样,这里的时间单位可以是 minutes 、hours 、 days 或 weeks, //at now + 3 hours(从现在开始起三个小时后)
三:at服务的启动及查看
rpm -q at //查看at是否安装
rpm -ql at //查看at生成的文件列表
rpm -qc at //查看at的配置文件
centos7服务的查看
systemctl status atd //查看at的运行状态
systemctl start atd //启动at服务
systemctl restart atd //重启at服务
systemctl enable atd //开机自启
systemctl disable atd //开机禁用
centos6服务查看
service atd status //查看at的运行状态
service atd start //启动at服务
service atd restart //重启at服务
chkconfig atd on //开机自启
chkconfig atd off //开机禁用
at的白名单和黑名单
/etc/at.allow //默认不存在
/etc/at/deny //黑名单,添加用户进去之后此用户就不能使用at命令
四:周期性的计划任务
crontab //创建周期性的计划任务
crontab的用法
选项:
-e // 编辑当前用户的周期性计划任务
-l //列出所有的计划任务
-u //指定用户
-r //删除计划任务
例: crontab -e
*/5 * * * * tar -cvzf /etc/`date +\%F`_etc.tar.gz /etc/hosts //这里的命令一般用绝对路径,避免出错
删除计划任务
crontab -u test -r //查看test的疾患任务
周期性计划任务的启动及服务状态查看
查:
rpm -q crontabs //查看是否安装
rpm -ql crontabs //查看生成的文件列表
rpm -qc crontabs //查看配置文件列表
centos7服务;
systemctl status crond //查看状态
systemctl start crond //启动
systemctl restart crond //重启
systemctl enable crond //开机自启
systemctl disable crond //开机禁止
centos6服务
service crond status //查看状态
service crond start //启动
service crond restart //重启
chkconfig crond on //开机自启
chkconfig crond off //开机禁止
crontab的白名单和黑名单
/etc/corn.allow //白名单
/ect/cron.deny //黑名单 ,加入的用户不能使用crontab做计划任务
crontab的配置
cat /etc/crontab
SHELL=/bin/bash //shell环境
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
格式:分 时 日 月 周
以上是关于计划任务的主要内容,如果未能解决你的问题,请参考以下文章