9.5 (at crontab)

1、总结Linux系统上的任务计划(at、crontab)的详细使用方法;

计划任务分为一次性任务(at)和周期性任务(crontab)

at用法:

at [选项] 时间

-l   显示待执行任务  =  atq

-q QUEUE 指明作业队列

-d 编号 删除指定编号的待执行作业  = atrm

-c 编号  查看指定编号的待执行作业的详细内容

-f FILE  从指定的file中读取作业内容

时间分为相对时间、绝对时间、模糊时间

相对时间如:now+3minutes  now+#(minutes,hours,days,weeks)

绝对时间:HH:MM tomorrow  明天的几点几分   如 17:20 tomorrow,明天下午5点20分

模糊时间:midnight noon teatime(不常用)

at演示

[root@localhost ~]# at now+2minutes
at> ls /tmp
at> <EOT>
job 6 at 2015-09-07 09:40#ctrl+d 提交任务

[root@localhost ~]# at 17:00+3days
at> echo $PATH
at> <EOT>
job 8 at 2015-09-10 17:00

[root@localhost ~]# at 10:00 2015-09-10  #指明年月日时分的时候,要吧时间写在前面,日期写在后面
at> echo 'this is a test'
at> <EOT>
job 12 at 2015-09-10 10:00

[root@localhost ~]# atq  #查看任务
62015-09-07 09:40 a root
42015-09-28 09:32 a root

[root@localhost ~]# at -l  #查看任务=atq
62015-09-07 09:40 a root
42015-09-28 09:32 a root
[root@localhost ~]# 
[root@localhost ~]# atq
82015-09-10 17:00 a root
42015-09-28 09:32 a root
122015-09-10 10:00 a root
[root@localhost ~]# atrm 4  #等同于 at -d 4,删除编号为4的任务
[root@localhost ~]# atq
82015-09-10 17:00 a root
122015-09-10 10:00 a root
[root@localhost ~]# at -c 12 #查看编号为12的任务内容
#上面巴拉巴拉一大堆,省略(PATH,SHELL一些变量什么的)
SHELL:-/bin/sh} << 'marcinDELIMITER7b5db938'
echo 'this is a test'
marcinDELIMITER7b5db938

[root@localhost ~]# cat test.txt 
cat /et/sysconfig/network-scripts/ifcfg-eth0
cp -a /etc/rc.d/init.d/functions /tmp
[root@localhost ~]# at -f test.txt  now+5minutes#从指定文件中读取作业
job 13 at 2015-09-07 10:09


crontab用法:(服务名crond)

-e  edit 编辑任务

-l  list 列出任务

-r  remove 清空任务

-u user   管理指明用户的计划任务(root使用)

5个时间点说明 * * * * * 分别对应着分 时 日 月 周

取值范围:

分  0-59

0-23

日  1-31

1-12

周  0-7   0和7都表示星期日

*  代表所有可能的值,有着“每”的意思

-  一个连续的时间  2-6表示 2,3,4,5,6

,  离散的时间   1,3,5 

/#  有效时间范围内每#时间,用于指定频率  */3  * * * * 表示每三分钟,有效时间一定要能整除# 如果分钟位写成*/8,每8分钟执行,但最后一次不是8分钟,取值范围是0-59不能整除8


2、每周一到周六的凌晨3点20分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD;

cp改为tar压缩归档

[root@localhost backup]# crontab -l

20 3  * * 1-6  tar -zcvf  /backup/etc-`date +\%F`.tar.gz  /etc    #任务计划中%需要转意


3、每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss;

30 2  * * 0    tar -zcvf /backup/fstab-`date +\%Y-\%m-\%d-\%H-\%M-\%S`.tar.gz /etc/fstab


4、每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线;

0 0 * * *   echo "===========`date +\%F`===========" >> /statistics/meminfo.txt  && grep '^[SM]' /proc/meminfo >> /statistics/meminfo.txt










你可能感兴趣的:(总结,练习)