Ubuntu 設置定時crontab任務

crontab屬於常見的自動化運維命令


安裝及運行

apt-get install cron
service cron start #啓動cron服務
service cron status #查看cron服務狀態
cron start/running, process 8876

使用cron的幫助信息

man crontab #詳述了crontab的語法
 
CRONTAB(1)                                                                      General Commands Manual                                                                      CRONTAB(1)
NAME
       crontab - maintain crontab files for individual users (Vixie Cron)
SYNOPSIS
       crontab [ -u user ] file
       crontab [ -u user ] [ -i ] { -e | -l | -r }
DESCRIPTION
       crontab is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron.  Each user can have their own crontab, and though these are
       files in /var/spool/cron/crontabs, they are not intended to be edited directly.
       If the /etc/cron.allow file exists, then you must be listed (one user per line) therein in order to be allowed to use this command.  If the /etc/cron.allow file does not  exist
       but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command.
       If  neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able
       to use this command.
       If both files exist then /etc/cron.allow takes precedence. Which means that /etc/cron.deny is not considered and your user must be listed in /etc/cron.allow in order to be able
       to use the crontab.
       Regardless  of the existance of any of these files, the root administrative user is always allowed to setup a crontab.  For standard Debian systems, all users may use this com‐
       mand.
       If the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing). If this option is not given,  crontab  exam‐
       ines  "your"  crontab, i.e., the crontab of the person executing the command.  Note that su(8) can confuse crontab and that if you are running inside of su(8) you should always
       use the -u option for safety's sake.
       The first form of this command is used to install a new crontab from some named file or standard input if the pseudo-filename ``-'' is given.
       The -l option causes the current crontab to be displayed on standard output. See the note under DEBIAN SPECIFIC below.
       The -r option causes the current crontab to be removed.
       The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.  After  you  exit  from  the  editor,  the  modified
       crontab will be installed automatically. If neither of the environment variables is defined, then the default editor /usr/bin/editor is used.
       The -i option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.
DEBIAN SPECIFIC
       The  "out-of-the-box"  behaviour for crontab -l is to display the three line "DO NOT EDIT THIS FILE" header that is placed at the beginning of the crontab when it is installed.
       The problem is that it makes the sequence
       crontab -l | crontab -
       non-idempotent -- you keep adding copies of the header. This causes pain to scripts that use sed to edit a crontab. Therefore, the default behaviour of the -l option  has  been

用法解析

crontab [ -u user ] file #加載crontab任務時加載file文件,如果不指定文件則接受鍵入的命令
crontab [ -u user ] [ -i ] { -e | -l | -r }
crontab -l #顯示系統cron任務
crontab -l -u username #顯示用戶username的cron任務
crontab -e #編輯當前用戶的cron任務
crontab -e -u root #編輯root用戶cron任務
crontab -r #刪除當前用戶cron任務
crontab -r -u username #刪除用戶username全部cron任務
crontab -ir #刪除全部cron任務且無需確認

添加crontab任務

建議直接添加系統級cron任務以避免不同用戶導致的不執行的bug

編輯cron文件

sudo vi /etc/crontab #開始編輯
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
0  2    * * *   root    find /home/svnuser/csvn/data/dumps/ -type f -mtime +7 -exec rm -f {} \;
#delete overtime old dump.zip by xxxx 2016-9-22 10:53:29

在末尾註釋上一行添加cron任務

# m h dom mon dow user  command
0  2    * * *   root    find /home/svnuser/csvn/data/dumps/ -type f -mtime +7 -exec rm -f {} \;

解析:在每天的凌晨2點0分,以root權限刪除svn備份中的超過7天的zip文件,手動很麻煩,cron很適合

# m h dom mon dow user  command
  | |  |   |   |   |       |--所需執行的命令
  | |  |   |   |   |--對應執行命令的用戶,比如root/username
  | |  |   |   |--星期0~7,可以多選,比如1,4-表示週一及週四,週日0/7同義,,*表示每週
  | |  |   |--月份1~12,*表示每月
  | |  |--日起1~31,*表示每日
  | |--小時1~23,0表示0點
  |--分鐘1~59,*或者*/1表示每分鐘

配置完成後保存,重啓cron服務

sudo service cron restart

查看cron任務

sudo crontab -l #此時不會顯示系統級cron任務,只顯示用戶級,請參考crontab -e命令設置

ps:部分情況下,cron在執行任務時需要環境變量,此時需要在運行命令之前先運行環境變量。


2016-9-22

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章