一次性任务
at命令
at [HH:MM] [yyyy-mm-dd]
使用at命令设置的计划任务至在指定的时间执行一次。前提是对于的系统服务atd必须已经运行。
CTRL +D提交任务。
at now+2min :2分钟之后执行命令
at teatime :下午茶时间。默认4点
两分钟后查看系统时间
at teatime tomorrow :明天下午茶
at teatime +3day :三天后
at 3pm:下午三点
atrm 任务号:删除任务
查询一次性任务:atq
周期性任务设置
crontab命令
使用crontab命令设置的计划任务可以按预约的周期重复执行。
crontab的配置文件和目录
/etc/crontab 文件中设置的是维护Linux系统所需的任务,由Linux系统及相关程序在安装时自动设置。
[root@localhost spool]# 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) #分钟(0-59)
# | .------------- hour (0 - 23) #小时(0-23)
# | | .---------- day of month (1 - 31) #日(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
crontab -e :出现编辑文件
0 10 10 * * 、user/bin/cp -p /etc/hostname /opt :在每月10号的10点 拷贝以下内容到/opt内。
任务一定要用绝对路径。查询绝对路径:which cp
crontab -l:查询任务
crontab -r:将任务全删除。删除单个可以在编辑器内删除
脚本任务: /bin/sh
[root@localhost ~]# vim time.sh
#! /bin/bash
echo "hello world"
root用户可以管理其他用户命令
crontab -u xxx -e :更改xxx用户的周期性命令。
crontab -u xxx -r :删除xxx用户的计划任务
黑白名单
vim /etc/cron.allow :允许写crontab命令的名单。系统默认没有,需要自己创建
vim /etc/cron.deny :拒绝crontab命令的名单。有allow先看allow,如果名单时在两个名单中,可以执行。
使用crontab命令管理永恒的计划任务
– e:编辑计划任务列表
– u:指定所管理的计划任务属于哪个用户。
– l:列表显示计划任务
– r:删除计划任务表
当使用“*”时表示去痣范围中的任意时间。crontab任务配置记录中所设置的命令操作将在“分钟”+“小时”+“日期”+“月份”+“周”
还可以使用“-” “,”“/”与数字构成表达式来表示较复杂的时间关系: