系统相关
首页 > 系统相关> > Linux-任务计划

Linux-任务计划

作者:互联网

1、一次性任务

at 工具

at 命令:

at [option] TIME
-V 显示版本信息
-t time   时间格式 [[CC]YY]MMDDhhmm[.ss]
-l 列出指定队列中等待运行的作业;相当于atq
-d N 删除指定的N号作业;相当于atrm
-c N 查看具体作业N号任务
-f file 指定的文件中读取任务
-m 当任务被完成之后,将给用户发送邮件,即使没有标准输出
常用选项:

注意:

TIME:定义出什么时候进行 at 这项任务的时间

HH:MM 在今日的 HH:MM 进行,若该时刻已过,则明天此时执行任务
02:00    
HH:MM YYYY-MM-DD   规定在某年某月的某一天的特殊时刻进行该项任务
02:00 2016-09-20  
HH:MM[am|pm] [Month] [Date]
06pm March 17
17:20 tomorrow
HH:MM[am|pm] + number [minutes|hours|days|weeks], 在某个时间点再加几个时间后才进行该
项任务
now + 5 min
02pm + 3 days
范例:at 时间格式

at 任务执行方式:

/etc/at.{allow,deny} 控制用户是否能执行at任务

[root@ubuntu ~]#ll /var/spool/cron/
total 20
drwxr-xr-x 5 root   root    4096 Apr 23  2020 ./
drwxr-xr-x 5 root   root    4096 Dec 12 17:42 ../
drwxrwx--T 2 daemon daemon  4096 Dec 12 17:47 atjobs/
drwxrwx--T 2 daemon daemon  4096 Dec 12 17:47 atspool/
drwx-wx--T 2 root   crontab 4096 Feb 14  2020 crontabs/
[root@ubuntu ~]#ll /var/spool/cron/atjobs/
total 16
drwxrwx--T 2 daemon daemon 4096 Dec 12 17:47 ./
drwxr-xr-x 5 root   root   4096 Apr 23  2020 ../
-rwx------ 1 root   daemon 2875 Dec 12 17:36 a000010198e058*
-rw------- 1 daemon daemon    6 Dec 12 17:46 .SEQ
范例: ubuntu at任务存放路径

2、周期性任务计划cron

周期性任务计划cron相关的程序包:

cron 依赖于crond服务,确保crond守护处于运行状态:

#CentOS 7 以后版本:
systemctl status crond
#CentOS 6:
service crond status

cron任务分为

计划任务日志:/var/log/cron

创建周期计划任务crontab

/etc/crontab 格式说明

[root@centos8-liyj ~]#cat /etc/crontab 
SHELL=/bin/bash                #默认的SHELL类型
PATH=/sbin:/bin:/usr/sbin:/usr/bin     #默认的PATH变量值,可修改为其它路径
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

crontab命令格式:

crontab [-u user] [-l | -r | -e] [-i]

常用选项:

-l 列出所有任务
-e 编辑任务
-r 移除所有任务
-i 同-r一同使用,以交互式模式移除指定任务
-u user 指定用户管理cron任务,仅root可运行

11月每天的6-12点之间每隔2小时执行/app/bin/test.sh

#在6,8,10,12点整共4次分别执行test.sh
[root@centos8 ~]#crontab -l
0 6-12/2 * 11 * /app/bin/test.sh

#以下配置只会在5,7,9,11点整执行
0 5-12/2 * 11 * /app/bin/test.sh

 

标签:daemon,12,crontab,cron,任务,计划,Linux,root
来源: https://www.cnblogs.com/lyj1023/p/16201981.html