1、crontab介绍
在LINUX中,周期执行的任务一般由cron这个守护进程来处理ps -ef|grep cron
。cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间
cron的配置文件称为“crontab”,是“cron table”的简写
单个用户的计划任务 | 系统级别的计划任务 |
---|---|
单个用户生效(单一一次,周期执行) | 所有用户都去执行(大多数是周期性执行) |
2、单一一次执行
at now + 2 minutes 或者 at 10:23 月 日 年
>输入要执行的命令
>CTRL+d 输入结束
查看:
atq :查看设置的任务计划,执行后就不会显示了
/var/apool/at
:当计划任务没有执行会在该目录下产生一个普通文件,但是执行后该文件也就不存在了
删除:
atrm 或者 删除/var/spool/at/
下的执行计划
一分钟后广播: hello kugou!!
[root@localhost ~]# at now + 1 minutes
at> wall hello kugou!! #wall 广播
at> <EOT> #CTRL+D 结束任务
job 2 at Tue Aug 20 10:07:00 2019
[root@localhost ~]# atq #查看执行任务
2 Tue Aug 20 10:07:00 2019 a root
[root@localhost ~]# cd /var/spool/at/ #查看执行任务
[root@localhost at]# ll
total 4
-rwx------. 1 root root 3461 Aug 20 10:06 a00002018e527f
drwx------. 2 root root 6 Aug 20 10:04 spool
[root@localhost at]#
Broadcast message from root@localhost.localdomain (Tue Aug 20 10:07:01 2019):
hello kugou!!
3、周期性执行计划任务
创建cron表,通过命令crontab -e
来创建
* * * * * user-name command to be executed
* * * * * 分 时 日 月 周(五个*号作用)
minute:表示分钟,可以从0 -- 59 之间任何整数。 */1每隔一分钟执行一次
hour:表示小时,可以从0 -- 23 之间的任何数字
day:表示日期,可以从1 -- 31 之间的任何整数
mouth:表示月份,从1 -- 12之间任何整数
week:表示星期几,可以从0 -- 7 之间的任何整数 0 == 7(周天)
command:要执行的命令,可以是系统命令,也可以时自己编写的脚本
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# crontab -l -u root
06 14 * * 03 wall hello kugou!!
[root@localhost ~]# cd /var/spool/cron/
[root@localhost cron]# ll
total 4
-rw-------. 1 root root 32 Aug 20 10:15 root
crontab -e
(-u user 不写就是当前用户)
查看:crontab -l
按条删除:crontab -e
进入执行表中删除某条
删除全部: 删除/var/spool/cron/
文件、crontab -r
清除我的所有计划任务列表
[root@localhost ~]# vim /etc/crontab
SHELL=/bin/bash #命令解释器
sbin:/bin:/usr/sbin:/usr/bin #脚本所执行的对应的命令的路径
MAILTO=root #邮件信息给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
4、特殊情况
未正常执行任务(断电或其他情况),在开机后检查 计划任务,那些没有执行,然后在指定时间内去执行
[root@localhost cron]# vim /etc/anacrontab
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily #每天的任务:开机后检查是否执行过,未执行则在5分钟内执行
7 25 cron.weekly nice run-parts /etc/cron.weekly #每周的任务:开机后检查7天内是否执行过,未执行则在25分钟内执行
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly #每月的任务:开机后检查1个月内是否执行过,未执行则在45分钟内执行
~
当我们同一时刻执行多个脚本 run-parts
,可以执行一个目录下的所有脚本或程序usr/bin/run-parts
将shell脚本放置在此目录下
5、拒绝执行
拒绝某个用户执行单一一次的任务计划:
[root@localhost cron]# vim /etc/at.deny
redhat
[redhat@localhost ~]$ at now + 1 minutes
You do not have permission to use at.
拒绝某个用户执行周期的任务计划:
[root@localhost cron]# vim /etc/cron.deny
[redhat@localhost ~]$ crontab -e
You (redhat) are not allowed to use this program (crontab)
See crontab(1) for more information
配置crontab -e 和 /etc/crontab的区别:
crontab -e
:是当前用户定义的,只对当前用户生效
/etc/crontab
:是针对系统所有用户生效的