Linux計劃任務

一 Linux 郵件系統shell

     mail    直接輸入查看郵件
     mail  root@localhost 正文交互式提供 Ctrl +d 提交
         -s  -s subject
         -a file
   
  例vim

  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')"bash

輸入數字查看郵件詳情ide

輸入d 1 刪除第一個郵件工具

按Ctrl+d 退出spa

 

二 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 命令
   .net

 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 crontabscrontab

   # 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補充工具

 

練習

 二、每週一到週六的凌晨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)

三、每週日凌晨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)

  四、天天晚上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
相關文章
相關標籤/搜索