Linux学习之任务计划(at、batch、crontab)篇
一、linux上的任务计划
1、分类
linux上的任务计划可以分为两类:
a、周期性执行的任务计划
crontab
b、后续时间一次性执行的计划
at、batch
2、电子邮件服务
电子邮件服务使用到的几个协议:
smtp:simple mail transmission protocol
pop3:post office protocol
imap4:internet mail access protocol
没个用户的都有一个专门存放邮件的地方:
/var/spool/mail/USERNAME
[root@pizi mail]# ll 总用量 12 -rw-rw----. 1 3001 mail 0 9月 1 23:33 123user -rw-r--r--. 1 root root 11 9月 5 15:40 at.list -rw-rw----. 1 hadoop mail 0 9月 5 11:01 hadoop -rw-rw----. 1 600 mail 0 8月 27 00:40 piziuser -rw-------. 1 root mail 4773 9月 5 17:02 root -rw-rw----. 1 rpc mail 0 8月 24 23:03 rpc -rw-rw----. 1 usera mail 0 9月 4 20:23 usera -rw-rw----. 1 userb mail 0 9月 4 20:58 userb -rw-rw----. 1 userc mail 0 9月 4 20:58 userc [root@pizi mail]#
mail命令:
mail:进入交互式收发邮件接口;
[root@pizi mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 5 messages > 1 Mail Delivery System Sat Sep 5 15:22 75/2399 "Undelivered Mail Retu" 2 usera@pizi.pizilinux Sat Sep 5 15:23 19/557 "hi" 3 root Sat Sep 5 15:29 37/565 "Output from your job " 4 root Sat Sep 5 15:44 15/544 "Output from your job " 5 Cron Daemon Sat Sep 5 16:30 23/708 "Cron <root@pizi> /bin" &
mail -s 'SUBJECT' username@hostname
[root@pizi mail]# mail -s 'test mail' root@pizi.pizilinux this is test mail EOT [root@pizi mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 10 messages 1 new 1 Mail Delivery System Sat Sep 5 15:22 75/2399 "Undelivered Mail Retu" 2 usera@pizi.pizilinux Sat Sep 5 15:23 19/557 "hi" 3 root Sat Sep 5 15:29 37/565 "Output from your job " 4 root Sat Sep 5 15:44 15/544 "Output from your job " 5 Cron Daemon Sat Sep 5 16:30 23/708 "Cron <root@pizi> /bin" 6 Mail Delivery System Tue Sep 8 22:33 75/2402 "Undelivered Mail Retu" 7 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 8 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 9 Mail Delivery System Tue Sep 8 22:35 74/2324 "Undelivered Mail Retu" >N 10 root Tue Sep 8 22:37 18/580 "test mail" &
mail -s 'SUBJECT' username@hostname < /path/from/somefile
[root@pizi mail]# cat /tmp/testmail.txt this is a test mail txt file! [root@pizi mail]# mail -s 'txt' root@pizi.pizilinux < /tmp/testmail.txt [root@pizi mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 11 messages 1 new 2 unread 1 Mail Delivery System Sat Sep 5 15:22 75/2399 "Undelivered Mail Retu" 2 usera@pizi.pizilinux Sat Sep 5 15:23 19/557 "hi" 3 root Sat Sep 5 15:29 37/565 "Output from your job " 4 root Sat Sep 5 15:44 15/544 "Output from your job " 5 Cron Daemon Sat Sep 5 16:30 23/708 "Cron <root@pizi> /bin" 6 Mail Delivery System Tue Sep 8 22:33 75/2402 "Undelivered Mail Retu" 7 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 8 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 9 Mail Delivery System Tue Sep 8 22:35 74/2324 "Undelivered Mail Retu" U 10 root Tue Sep 8 22:37 19/590 "test mail" >N 11 root Tue Sep 8 22:41 18/586 "txt" & & 11 Message 11: From root@pizi.pizilinux Tue Sep 8 22:41:32 2015 Return-Path: <root@pizi.pizilinux> X-Original-To: root@pizi.pizilinux Delivered-To: root@pizi.pizilinux Date: Tue, 08 Sep 2015 22:41:32 +0800 To: root@pizi.pizilinux Subject: txt User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@pizi.pizilinux (root) Status: R this is a test mail txt file! &
COMMAND | mail -s 'SUBJECT' username@hostname
[root@pizi mail]# ifconfig | mail -s 'ifconfig' root@pizi.pizilinux [root@pizi mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 12 messages 1 new 2 unread 1 Mail Delivery System Sat Sep 5 15:22 75/2399 "Undelivered Mail Retu" 2 usera@pizi.pizilinux Sat Sep 5 15:23 19/557 "hi" 3 root Sat Sep 5 15:29 37/565 "Output from your job " 4 root Sat Sep 5 15:44 15/544 "Output from your job " 5 Cron Daemon Sat Sep 5 16:30 23/708 "Cron <root@pizi> /bin" 6 Mail Delivery System Tue Sep 8 22:33 75/2402 "Undelivered Mail Retu" 7 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 8 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 9 Mail Delivery System Tue Sep 8 22:35 74/2324 "Undelivered Mail Retu" U 10 root Tue Sep 8 22:37 19/590 "test mail" 11 root Tue Sep 8 22:41 19/597 "txt" >N 12 root Tue Sep 8 22:42 35/1448 "ifconfig" & 12 Message 12: From root@pizi.pizilinux Tue Sep 8 22:42:58 2015 Return-Path: <root@pizi.pizilinux> X-Original-To: root@pizi.pizilinux Delivered-To: root@pizi.pizilinux Date: Tue, 08 Sep 2015 22:42:57 +0800 To: root@pizi.pizilinux Subject: ifconfig User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: root@pizi.pizilinux (root) Status: R eth0 Link encap:Ethernet HWaddr 08:00:27:A1:56:BC inet addr:192.168.3.8 Bcast:192.168.3.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fea1:56bc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1398 errors:0 dropped:0 overruns:0 frame:0 TX packets:975 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:135985 (132.7 KiB) TX bytes:129159 (126.1 KiB)
3、at命令
at命令是承载未来时间运行的某作业:
支持使用作业队列:
默认为a队列;
使用方法:
at [option]... TIME
[option]选项
-q QUEUE :at的作业队列;
-f /path/from/somefile:从指定的文件中读取要运行的作业;
-l:查看作业队列中德等待运行的作业,相当于使用atq命令;
-c AT JOB_NUM:查看待运行作业的内容
-d:删除指定作业;
TIME时间
a、绝对时间:
HH:MM,MMDD[CC]YY,MM/DD/[CC]/YY
b、相对时间:
now+#unit
unit:minute,hour,day,week
c、模糊时间:
midnight、noon、teatime
[root@pizi mail]# at now+1minute at> ls / at> <EOT> job 4 at 2015-09-08 22:52 [root@pizi mail]# at -l 4 2015-09-08 22:52 a root [root@pizi mail]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 13 messages 1 new 2 unread 1 Mail Delivery System Sat Sep 5 15:22 75/2399 "Undelivered Mail Returned to Sender" 2 usera@pizi.pizilinux Sat Sep 5 15:23 19/557 "hi" 3 root Sat Sep 5 15:29 37/565 "Output from your job 2" 4 root Sat Sep 5 15:44 15/544 "Output from your job 3" 5 Cron Daemon Sat Sep 5 16:30 23/708 "Cron <root@pizi> /bin/echo "hello"" 6 Mail Delivery System Tue Sep 8 22:33 75/2402 "Undelivered Mail Returned to Sender" 7 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 8 usera@pizi.pizilinux Tue Sep 8 22:34 19/567 "hi" 9 Mail Delivery System Tue Sep 8 22:35 74/2324 "Undelivered Mail Returned to Sender" U 10 root Tue Sep 8 22:37 19/590 "test mail" 11 root Tue Sep 8 22:41 19/597 "txt" 12 root Tue Sep 8 22:42 36/1459 "ifconfig" >N 13 root Tue Sep 8 22:52 36/554 "Output from your job 4" & 13 Message 13: From root@pizi.pizilinux Tue Sep 8 22:52:03 2015 Return-Path: <root@pizi.pizilinux> X-Original-To: root Delivered-To: root@pizi.pizilinux Subject: Output from your job 4 To: root@pizi.pizilinux Date: Tue, 8 Sep 2015 22:52:03 +0800 (CST) From: root@pizi.pizilinux (root) Status: R bin boot dev etc home lib lib64 lost+found media misc mnt net opt proc root sbin selinux srv sys test tmp usr var
4、batch:
由系统自行选择在资源较为空闲时运行指定的任务:
5、crontab:周期性的任务计划
其守护进程为crond
周期性任务计划分为两类:
a、系统cron任务:没有默认运行用户身份,所以需要额外指定运行者;
# 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)
# | | | | |
# * * * * * user-name command to be executed
系统性cron任务由7个字段组成:
前5个字段:时间点
user-name:计划时间点以哪个用户运行程序;
command to be executed:要运行的任务;
时间表示法:
*,表示该时间字段取值范围内的所有值
-:一个特定连续时间范围;3-7
,:一个离散的时间点,1,3,4,5
/#:有限时间范围内的每多少时间,用于指定频率;
注意:day of month ,month 两个时间点一般不与day of week同时使用
例:
[root@pizi mail]# crontab -l 5 */3 * * * root /bin/echo "hello world!"
表示每3个小时的第5分钟执行一次echo "hello world"命令
b、用户cron任务:由某个用户所提交,默认就是以提交者的身份运行,无需额外执行运行者:
# 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)
# | | | | |
# * * * * * command to be executed
此处的模式跟系统cron任务唯一不同的是此处不用指定运行用户了。其他都同上
c、crontab命令的用法:
crontab [-u user] [-l|-r|-e]
-u user:不是管理自己的cron任务,而是指定的目标用户的cron任务,仅root拥有管理其他用户的cron任务的权限,默认管理自己的;
-l:list 列出任务;
-r:remove,移除所有任务;
-e:edit,编辑,打开一当前shell回话的默认编辑器来编辑cron任务表;
d、注意:
1)、如果不想接收任务执行结果的通知邮件:
command > /dev/null :表示不接收执行任务正确的邮件通知
command &> /dev/null :表示不接收所有的邮件通知
2)、对于crontab文件来讲,%有特殊的功用,如果命令中出现了%,要记得转义,或者用单引号对其引用;
3)、crondtab的path变量与用户的变量不完全相同,所以建议在cron中的任务要使用绝对路径来执行
二、练习:
1、每周一到周六的凌晨3点20分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD;
[root@pizi backups]# crontab -l 20 3 * * 1-6 /bin/cp -af /etc/ /backups/etc-$(date +\%F)
2、每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss
[root@pizi backups]# crontab -l 20 3 * * 1-6 /bin/cp -af /etc/ /backups/etc-$(date +\%F) 30 2 * * 0 /bin/cp -af /etc/fstab /backups/fstab-$(date +\%F-\%H-\%M-\%S)
3、每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;
[root@pizi statistics]# crontab -l 20 3 * * 1-6 /bin/cp -af /etc/ /backups/etc-$(date +\%F) 30 2 * * 0 /bin/cp -af /etc/fstab /backups/fstab-$(date +\%F-\%H-\%M-\%S) 0 23 * * * (/bin/echo "============";/bin/egrep "^(S|M)" /proc/meminfo) >> /statistics/meminfo.txt