您的位置:首页 > 运维架构 > Linux

7.3Linux中的任务计划实现详解

2016-04-23 00:00 316 查看
一次性任务执行:at, batch
周期性任务执行:crontab

一次性任务执行:
at TIME 指定时间后,按回车
at> ls /etc
at> ls -l /var
at> wc -l /etc/fstab
at>
Ctrl+d:提交任务到后台执行

TIME:
模糊时间:
now+#UNIT
noon 中午12点
midnight 午夜时分
teatime 下午茶
tomorrow
hh:mm
[root@root ~]#at now+2min
at> cat /ert^H^[[3~^[[3~^[[3~^K^H
at> ^C[root@root ~]#at now+2min
at> ls /var/
at> cat /etc/issue
at> <EOT>
job 2 at 2014-12-31 08:54
[root@root ~]#at -l
2 2014-12-31 08:54 a root
2 两个作业
2014-12-31 08:54 执行时间
a 作业队列
root root身份执行

at有作业队列:
使用单个字母表示

atq: 查看作业队列中的待执行任务
at -l
[root@root ~]#atq
You have new mail in /var/spool/mail/root
[root@root ~]#at -l
[root@root ~]#date
Wed Dec 31 08:56:53 CST 2014
[root@root ~]#atq
任务作业执行完成后:结果会通过邮件通知给作业提交者

CentOS: 默认会安装并启动邮件服务,仅服务于本地系统;
# mail
& delete 1-40
& help
mail commands
type <message list> type messages
next goto and type next message
from <message list> give head lines of messages
headers print out active message headers
delete <message list> delete messages
undelete <message list> undelete messages
save <message list> folder append messages to folder and mark as saved
copy <message list> folder append messages to folder without marking them
write <message list> file append message texts to file, save attachments
preserve <message list> keep incoming messages in mailbox even if saved
Reply <message list> reply to message senders
reply <message list> reply to message senders and all recipients
mail addresses mail to specific recipients
file folder change to another folder
quit quit and apply changes to folder
xit quit and discard changes made to folder
! shell escape
cd <directory> chdir to directory or home if none given
list list names of all available commands

A <message list> consists of integers, ranges of same, or other criteria
separated by spaces. If omitted, mail uses the last message typed.
实现快速邮件发送
mail -s "标题信息" 用户 < 文件
echo "正文" | mail -s "标题信息" 用户
at -d = atrm 删除一个作业,指定作业编号

batch: 功能同at, 但无须为其指定时间,其会选择系统资源较空闲时执行指定的作业

周期性任务执行:cron, centos 5上使用vixie-cron包来实现其功能的, centos 6上安
装的cronie
crond: cron daemon, cron守护进程,一直工作于后台

# service crond status
crond (pid 1718) is running...

如果状态信息为“is stopped.”,则需要执行“service crond start; chkconfig crond on”;

cron任务分两类:
系统cron: 定义在/etc/crontab
用户cron: 定义在/var/spool/cron目录中,每个用户都有一个与用户名同名的文件,其
功能类似于/etc/crontab

每行定义一个独立的任务:
[root@root ~]#cat /etc/crontab
SHELL=/bin/bash 用什么shell来执行命令
PATH=/sbin:/bin:/usr/sbin:/usr/bin 指定查找路径的位置
MAILTO=root
HOME=/

# 环境变量

# 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

注意:如果定义在cron中的任务是脚本,此脚本中的各命令查找时遵循的是cron的PATH环境
变量定义;因此,在命令行中测试无问题的脚本,在cron中未必;
解决方案:
命令使用绝对路径
在脚本中开始自己定义一个专用PATH环境变量

用户cron的每个作业由2部分组成
时间
分钟 小时 日 月 周几
命令(或特定脚本)

时间表示法:
1、每个时间位都应该使用其可用的有效取值范围内的值
3 * * * * /bin/echo "howdy" 每个小时的第3分钟执行一次
17 3 * * * /bin/echo "howdy"
7 6 5 * * /bin/echo "howdy"
7 6 5 4 *

1 9 * * 3 /bin/echo "howdy"
2、某个时间位上的*表示对应时间位的所有有效取值;
3、*/#: 在对应的时间位的有效取值上每#一次;
*/3 * * * *
0 */3 * * *
*/8 * * * *: ?
0-59 从整除的时间开始算
0, 8, 16, 24, 32, 40, 48, 56,
4、某个时间位上的连续区间:-
10-50/3 * * * * 10-50分钟上每3分钟执行一次
5、离散取值:,
1 7,9,13 * * *

正确输出时,丢弃邮件通知:
输出重定向:
> /dev/null
&> /dev/null
> /dev/null 2>&1

用户cron任务的定义方式:
crontab
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
-l: 列出已经定义的所有任务
-l The current crontab will be displayed on standard output.
-e: 打开编辑界面定义任务
-e This option is used to edit the current crontab using the
editor specified by the VISUAL or EDITOR environment vari-
ables. After you exit from the editor, the modified
crontab will be installed automatically.
-r: 移除所有任务
-r The current crontab will be removed. 会把/var/spool/cron/root文件删除

crontab -u USERNAME -e

[root@root ~]#cat /var/spool/cron/root
15 2 * * */2 /usr/bin/yum -y update > /dev/null
20 3 * * */2 /usr/bin/updatedb > /dev/null

注意:如果在crontab的用户命令中使用%,得转义,使用\%的格式;不过,如果使用单引号引起来,也可不用转义;
5 3 * * * /bin/touch /tmp/testfile-$(date +\%F-\%H-\%M-\%S)
6 4 * * * /bin/touch /tmp/testfile-$(date +'%F-%H-%M-%S')

练习:
1、每4小时执行一次对/etc/目录的备份,备份至/backup目录中,保存的目录名为etc-2014071004;
0 */4 * * * [ -d /backup ] || mkdir /backup; /bin/cp -a /etc /backup/etc-$(date +'%Y%m%d%H')

2、每周3,5,7备份/var/log/messages文件至/backup/message_logs/目录中,保存的文件名为messages-2014071004;
3 1 * * 3,5,7

3、每天每两小时取当前系统/proc/meminfo中的以S开头的信息保存至/stats/memory.txt中
2 */2 * * * grep -i "^S" /proc/meminfo >> /stats/memory.txt

4、工作日的工作时间内,每小时执行一次'echo "howdy"'
10 8-18 * * 1-5 echo "howdy"
bash编程中:
逻辑操作:
&&: condition1 && condition2
||: condition1 || condition2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: