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