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

Linux计划任务

2015-09-07 22:42 591 查看
一 Linux 邮件系统
mail 直接输入查看邮件
mail root@localhost 正文交互式提供 Ctrl +d 提交
-s -s subject
-a file


mail -s 'fstable' root@localhost < /etc/fstable
cat /etc/fstab | mail -s "fstab.net"
mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 3 messages
> 1 (Cron Daemon) Mon Sep 7 21:36 28/1067 "Cron <root@localhost> /usr/bin/cp -airf /etc /tmp/etc-$(date +'"
2 (Cron Daemon) Mon Sep 7 21:37 28/1067 "Cron <root@localhost> '/usr/bin/cp -airf /etc /tmp/etc-$(date +"
3 (Cron Daemon) Mon Sep 7 21:40 28/1076 "Cron <root@localhost> /usr/bin/cp -airf /etc /tmp/etc-$(date +%Y-%m-%d')"
输入数字查看邮件详情
输入d 1 删除第一个邮件
按Ctrl+d 退出

二 Linux 上的计划任务
在未来某时间点一次性执行某任务 at batch
周期性的执行某任务 crontab

at
支持使用作业队列
默认为a队列
at now+3minute COMMAND
CTRAL+D 保存

at [-V] [-q queue] [-f file] [-mMlv] timespec...
at [-V] [-q queue] [-f file] [-mMkv] [-t time]
at -c job [job...]
atq [-V] [-q queue]
at [-rd] job [job...]
atrm [-V] job [job...]
batch
at -b
at 执行是否成功 ,会发送邮件

at [option] ... TIME
time 绝对时间 MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY or [CC]YY-MM-DD.
相对时间 now+# minute hour day week
模糊时间 midnight noon teatime tommorrow

-l 查看作业队列中等待运行的队列
-q queue at 作业队列
-f file 从指定文件读取要运行的作业,相当于使用atq 命令
-c 查看待运行作业的详细内容
-d 删除队列中的一个作业 相当于atrm 命令

batch
由系统自行选择在资源较为空闲的时间运行任务,其它和at一样

crontab 周期性任务计划
守护进程 crond

周期性任务有两类
1.系统cron任务,没有默认运行身份,需要额外指定的运行者
/etc/crontab
vim /etc/crontab

cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=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
2.用户cron任务,由某个用户提交,默认以提交者的身份运行,无需额外指定运行者
/var/spool/cron/USERNAME
# 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
# | | | | |
# * * * * * command to be executed

man 5 crontab
The time and date fields are:
field allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sunday, or use names)

*的时间表示
* 对应时间点有效取值范围内的每个时间点
- 一个特定连续时间范围 3-7
, 一个离散的时间点3,5,7
/# 有效时间范围内每多少时间,用于批定频率 1-30/4 */4

crontab 命令
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
crontab -n [ hostname ]
crontab -c

-l list 列出任务
-u user 不是管理自已的cron任务,管理目标用户的cron任务,仅root有管理其它用户cron任务的权限,默认管理自已的
-r remove 删除全部任务
-e edit 打开默认的编辑程序来编辑cron任务

注意
1.每8分钟运行一次任务 不能整除无法控制,要用shell script
2.每10秒种运行一次任务 要任务里多次运行
3.对于crontab文件来说,%有特殊功用,如果命令中会出现%,要转义,或者用单引号对其引用
4.crontab 的PATH变量与用户的变量不完全相同,所以 ,建议在cron中的命令要使用绝对路径

执行完成后 不想接收任务执行结果的通知邮件
command to be executed > /dev/null
command to be executed &> /dev/null

anacron crontab补充工具

练习
2、每周一到周六的凌晨3点20分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD;
cp -arf /etc /tmp/etc-$(date +%Y-%m-%d)
vim /etc/crontab
20 3 * * * root  /usr/bin/cp -airf /etc /tmp/etc-$(date +\%Y-\%m-\%d)
20 3 * * * root  /usr/bin/cp -airf /etc /tmp/etc-$(date +\%F)
3、每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss;
cp -irf /etc/fstab /tmp/fstab-$(date +%Y-%m-%d-%H-%M-%S)
mkdir /backup
vim /etc/crontab
30 2 * * * root  /usr/bin/cp -irf /etc /backup/fstab-$(date +\%Y-\%m-\%d-\%H-\%M-\%S)
30 2 * * * root  /usr/bin/cp -irf /etc /backup/fstab-$(date +\%F-\%H-\%M-\%S)
4、每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;
grep --color -E  '^(S|M)' /proc/meminfo
mkdir /statistics
vim /etc/crontab
0 0 * * * root   echo ===$(date +\%Y-\%m-\%d)=== >> /statistics/meminfo.txt ; grep --color -E  '^(S|M)' /proc/meminfo  >> /statistics/meminfo.txt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  crontab 计划任务