- 计划任务
计划任务
在指定的时间执行命令
- at - 将任务排队,在指定的时间执行。
- atq - 查看待执行的任务队列。
- atrm - 从队列中删除待执行的任务。
指定3天以后下午5点要执行的任务。
[root ~]# at 5pm+3daysat> rm -f /root/*.htmlat> <EOT>job 9 at Wed Jun 5 17:00:00 2019
查看待执行的任务队列。
[root ~]# atq9 Wed Jun 5 17:00:00 2019 a root
从队列中删除指定的任务。
[root ~]$ atrm 9
计划任务表 - crontab。
[root ~]# crontab -e* * * * * echo "hello, world!" >> /root/hello.txt59 23 * * * rm -f /root/*.log
说明:输入
crontab -e命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出hello, world!;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用搜索引擎找一下“Cron表达式在线生成器”来生成Cron表达式。和crontab相关的文件在
/etc目录下,通过修改/etc目录下的crontab文件也能够定制计划任务。[root ~]# cd /etc[root etc]# ls -l | grep cron-rw-------. 1 root root 541 Aug 3 2017 anacrontabdrwxr-xr-x. 2 root root 4096 Mar 27 11:56 cron.ddrwxr-xr-x. 2 root root 4096 Mar 27 11:51 cron.daily-rw-------. 1 root root 0 Aug 3 2017 cron.denydrwxr-xr-x. 2 root root 4096 Mar 27 11:50 cron.hourlydrwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.monthly-rw-r--r-- 1 root root 493 Jun 23 15:09 crontabdrwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.weekly[root etc]# vim crontab1 SHELL=/bin/bash2 PATH=/sbin:/bin:/usr/sbin:/usr/bin3 MAILTO=root45 # For details see man 4 crontabs67 # Example of job definition:8 # .---------------- minute (0 - 59)9 # | .------------- hour (0 - 23)10 # | | .---------- day of month (1 - 31)11 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...12 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat13 # | | | | |14 # * * * * * user-name command to be executed
