Linux任务计划

at任务

单次定时执行任务

at命令:at [option] TIME

u 常用选项:

-V 显示版本信息:

-l: 列出指定队列中等待运行的作业;相当于atq

-d: 删除指定的作业;相当于atrm

-c: 查看具体作业任务

-f /path/from/somefile:从指定的文件中读取任务

-m:当任务被完成之后,将给用户发送邮件,即使没有标准输出

u 注意:作业执行命令的结果中的标准输出和错误以邮件通知给相关用户

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

HH:MM [YYYY-mm-dd]

noon, midnight, teatime4pm

tomorrow

now+#{minutes,hours,days, OR weeks}

u执行方式:

1)交互式 2)输入重定向 3at –f 文件

uat队列存放在/var/spool/at目录中

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

白名单:/etc/at.allow 默认不存在,只有该文件中的用户才能执行at命令

黑名单:/etc/at.deny 默认存在,拒绝该文件中用户执行at命令,而没有在

at.deny 文件中的使用者则可执行

如果两个文件都不存在,只有 root 可以执行 at 命令

 

[root@centos7 17:21 
-Wat 
watt "helt time 
now" 
job 2 at Sun May 13 
17:26:00 
[root@centos7 17:22:16 
now +5 min 
2018 
Sun Nay 13 201B a root 
[root@centos7 17:22:27 now +6 min 
ping -c? -WI 172.20.0.1 
job 3 at sun hay 13 2018 
[root@centos7 -I 
sun hay 13 2018 a root 
Sun May 13 201B a root

 

 

周期性计划任务cron

 

命令及程序包

[root@centos7 17:37:35 —]#which crontab 
/usr/bin/crontab 
[ root@centos7 17:37:43 —]#rpm 
cronie-1.4.11-17. 
e17.x86 64 
[ root@centos7 17:37:46 —]#rpm 
Name 
: cronie 
Version 
. 1.4. 11 
Release 
. 17.e17 
Architecture : 
x86 64 
Install Date: 
G roup 
Size 
License 
Signature 
Source RPM 
Build Date 
Build Host 
Relocations . 
Packager 
Vendor 
URL 
Summa ry 
Description : 
-qf /usr/bin/crontab 
-qi cronie 
Wed 28 Mar 2018 AM CST 
System Environment/Base 
. 220388 
MIT and BSD and Isc and GPLv2+ 
RSA/SHA256, Thu 10 Aug 2017 PM CST, 
. cronie-1.4.11-17.e17.src. rpm 
Thu 03 Aug 2017 PM CST 
. clbm. rdu2.centos.org 
(not relocatable) 
: Cent0S BuildSystem <http://bugs.centos.org> 
. Cent0S 
. https://fedorahosted.org/cronie 
Key ID 24c6a8a7f4a80eb5 
: Cron daemon for executing programs at set times 
Cronie contains the standard UNIX daemon crond that runs specified programs at 
scheduled times and related tools. It is a fork of the original vixie-cron and 
has security and configuration enhancements like the ability to use pam and 
SELinux .

 

确保服务运行:

CentOS 7: systemctl status crond

CentOS 6: service crond status

[root@centos7 17:37 :54 —IWsystemctI status crond 
• crond. service 
Comand Scheduler 
Loaded : 
loaded (/usr/lib/systend/system/crond 
: active (running) since Sun 2018-05-13 
Active 
Main PID 
: 1092 (crond) 
CGroup : 
/ system. slice/crond. service 
Clog) /usr/sbin/crond •n

 

系统cron任务:系统维护作业

/etc/crontab文件中设置

SHELL 
=/bin/bash 
PATH=/ sbin : 'bin : /usr/sbin : /usr/bin 
MAIL To—root 
For details see man 4 crontabs 
Example of job definition: 
- minute (O 
• hour (O • 
- day Of nwnth (I 
• month (l 
• 12) OR 
- day Of week (O 
- 6) (Sunday=O or 7) OR sun , awn, tue,wed, thu, fri,sat 
user-narne command to be executed

其中本身带有配置信息的使用规则:

第一位*代表每分钟,可取值0-59,例如取值5则表示每小时的05分开始执行

第二位*代表每小时,可取值0-23,例如取值1则表示每天的1点(AM

第三位*代表每月的哪一号,可取值1-31

第四位*代表一年的哪一月份,可取值1-12

第五位*代表星期几,可取值(0-6)星期天是0

如果所在位数是*则代表,每xx时间执行,所有都是*则代表每分钟执行

 

例如,

下图第一个任务表示 65145分已root身份运行free -m >>  /data/memstat.log

第二个任务表示每个星期二的145分已root身份运行free -m >>  /data/memstat.log

SHELL=/bin/bash 
PATH—/sbin: /bin: / us r/ sbin: /usr/bin 
VIAILT0=root 
For details see man 4 crontabs 
Example Of job definition: 
• minute (O . 
- hour (O 
day of month (I • 
- month (I 
- 12) OR jan, 
user • name 
. • • day of 
root 
root 
free 
free 
week (O 
• 6) (Sunday—O or 7) OR sun, man, tue,wed, 
command to be executed 
/data/memstat. log 
/data/memstat log

 

用户cron任务:

因为这里是root执行crontab -e所以这里表示root用户每10分钟执行一次后面的任务

ucrontab命令定义

每个用户都有专用的cron任务文件: /var/spool/cron/USERNAME

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

u控制用户执行计划任务: /etc/cron.{allow,deny}

crontab -e 命令

(root@centos7 20:55:47 —J#crontab •e 
VIO * * * * bash /root/bin/95 diskcheck.sh /data/cron diskchk.tog

 


at

crontab


一次性作业使用

重复性作业使用crontab Create

Create

at time

crontab   -e

List

at -l

crontab   -l

Details

at -c jobnum

N/A

Remove

at -d jobnum

crontab   -r

Edit

N/A

crontab   -e

没有被重定向的输出会被邮寄给用户

根用户能够修改其它用户的作业

 

crontab的日志信息:/var/log/cron*

 


猜你喜欢

转载自blog.51cto.com/4081735/2115791