本章主要介绍如何创建计划任务
[root@node01 ~]# atq
[root@node01 ~]#
[root@node01 ~]# at -l
[root@node01 ~]#
下面开始创建at计划任务,at的用法如下。
at 时间点 <按【Enter】键>
> 输入耀执行的命令
Ctrl + D 提交
[root@node01 ~]# at 2023-12-10
warning: commands will be executed using /bin/sh
at> hostname
at>
job 1 at Sun Dec 10 11:06:00 2023
[root@node01 ~]#
at 时间 日期 <按【Ether】键>
>输入要执行的命令
Ctrl+D 提交
[root@node01 ~]# at 10:00 2023-12-10
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 2 at Sun Dec 10 10:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 10pm 2023-12-10
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 3 at Sun Dec 10 22:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 4pm +3days
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 4 at Mon Dec 11 16:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 4pm +1days
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 5 at Sat Dec 9 16:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 4pm tomorrow
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 6 at Sat Dec 9 16:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 10pm today
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 7 at Fri Dec 8 22:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at 12pm
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 9 at Fri Dec 8 12:00:00 2023
[root@node01 ~]#
[root@node01 ~]# at now+2hours
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 10 at Fri Dec 8 13:18:00 2023
[root@node01 ~]#
[root@node01 ~]# at now+3minutes
warning: commands will be executed using /bin/sh
at> rm -rf /root/apache-tomcat-8.5.77
at>
job 12 at Fri Dec 8 11:24:00 2023
[root@node01 ~]#
[root@node01 ~]# date ; ls /root/apache-tomcat-8.5.77
2023年 12月 08日 星期五 11:22:14 CST
bin CONTRIBUTING.md logs RELEASE-NOTES webapps
BUILDING.txt lib NOTICE RUNNING.txt work
conf LICENSE README.md temp
[root@node01 ~]#
[root@node01 ~]# date ; ls /root/apache-tomcat-8.5.77
2023年 12月 08日 星期五 11:25:54 CST
ls: 无法访问'/root/apache-tomcat-8.5.77': 没有那个文件或目录
[root@node01 ~]#
[root@node01 ~]# atq
1 Sun Dec 10 11:06:00 2023 a root
2 Sun Dec 10 10:00:00 2023 a root
3 Sun Dec 10 22:00:00 2023 a root
4 Mon Dec 11 16:00:00 2023 a root
5 Sat Dec 9 16:00:00 2023 a root
6 Sat Dec 9 16:00:00 2023 a root
7 Fri Dec 8 22:00:00 2023 a root
8 Fri Dec 8 22:00:00 2023 a root
9 Fri Dec 8 12:00:00 2023 a root
10 Fri Dec 8 13:18:00 2023 a root
[root@node01 ~]# at -l
1 Sun Dec 10 11:06:00 2023 a root
2 Sun Dec 10 10:00:00 2023 a root
3 Sun Dec 10 22:00:00 2023 a root
4 Mon Dec 11 16:00:00 2023 a root
5 Sat Dec 9 16:00:00 2023 a root
6 Sat Dec 9 16:00:00 2023 a root
7 Fri Dec 8 22:00:00 2023 a root
8 Fri Dec 8 22:00:00 2023 a root
9 Fri Dec 8 12:00:00 2023 a root
10 Fri Dec 8 13:18:00 2023 a root
[root@node01 ~]#
atrm N
或
at -d n
这里的N指的是atq查看的结果中前面的编号。
假设要删除编号玮10的at计划任务,命令如下
[root@node01 ~]# atrm 10
[root@node01 ~]# atrm {5..9}
[root@node01 ~]# atq
1 Sun Dec 10 11:06:00 2023 a root
2 Sun Dec 10 10:00:00 2023 a root
3 Sun Dec 10 22:00:00 2023 a root
4 Mon Dec 11 16:00:00 2023 a root
[root@node01 ~]#
这里N指的是 atq查看结果中前面的编号。
at -c N
[root@node01 ~]# at -c 2 | tail -3
${SHELL:-/bin/sh} << 'marcinDELIMITER025950c8'
ls /etc/hosts
marcinDELIMITER025950c8
[root@node01 ~]#
[bob@node01 ~]$ atq
[bob@node01 ~]$
[bob@node01 ~]$ at now+3hours
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 14 at Fri Dec 8 14:47:00 2023
[bob@node01 ~]$
[root@node01 ~]# echo bob > /etc/at.deny
[root@node01 ~]# cat /etc/at.deny
bob
[root@node01 ~]#
[bob@node01 ~]$ at now+3hours
You do not have permission to use at.
[bob@node01 ~]$
[root@node01 ~]# echo bob > /etc/at.allow
[root@node01 ~]# cat /etc/at.allow
bob
[root@node01 ~]#
[bob@node01 ~]$ at now+3hours
warning: commands will be executed using /bin/sh
at> ls /etc/hosts
at>
job 15 at Fri Dec 8 14:54:00 2023
[bob@node01 ~]$
[bob@node01 ~]$ crontab -l
no crontab for bob
[bob@node01 ~]$
[bob@node01 ~]$ crontab -l -u bob
must be privileged to use -u
[bob@node01 ~]$
分 时 天 月 周 命令
这几个时间单位可以用空格,也可以用【Tab】键来分隔。
* 7 * * * ls /etc/hosts
0 7 * * 1,2,3,4,5 ls /etc/hosts
0 7 * * 1-5 ls /etc/hosts
0 7 * 1-3 1-5 ls /etc/hosts
0 7 1-10 1-3 * ls /etc/hosts
0 7 1-10 1-3 1-5 ls /etc/hosts
[root@node01 ~]$ crontab -l
* 7 * * * ls /etc/hosts
0 7 * * * 1,2,3,4,5 ls /etc/hosts
0 7 * * 1-5 ls /etc/hosts
0 7 * 1-3 1-5 ls /etc/hosts
0 7 1-10 1-3 * ls /etc/hosts
0 7 1-10 1-3 1-5 ls /etc/hosts
[bob@node01 ~]$
[root@node01 ~]$ crontab -r
[root@node01 ~]# crontab -l
no crontab for root
[root@node01 ~]# cat /etc/cron.deny
[root@node01 ~]#
[root@node01 ~]# echo bob > /etc/cron.deny
[root@node01 ~]# cat /etc/cron.deny
bob
[root@node01 ~]#
[bob@node01 ~]$ crontab -e
You (bob) are not allowed to use this program (crontab)
See crontab(1) for more information
[bob@node01 ~]$
[root@node01 ~]# ls /etc/cron.allow
ls: 无法访问'/etc/cron.allow': 没有那个文件或目录
[root@node01 ~]# echo bob > /etc/cron.allow
[root@node01 ~]#
[root@node01 ~]# cat /etc/cron.deny
bob
[root@node01 ~]# cat /etc/cron.allow
bob
[root@node01 ~]#
[root@node01 ~]# ls /etc/cron*
/etc/cron.allow /etc/cron.deny /etc/crontab
/etc/cron.d:
0hourly raid-check
/etc/cron.daily:
logrotate
/etc/cron.hourly:
0anacron
/etc/cron.monthly:
/etc/cron.weekly:
[root@node01 ~]#