crontab詳解


crontab命令常見於Unix和Linux的操作系統之中,用於設置周期性被執行的指令。該命令從標准輸入設備讀取指令,並將其存放於“crontab”文件中,以供之后讀取和執行。通常,crontab儲存的指令被守護進程激活。crond常常在后台運行,每一分鍾檢查是否有預定的作業需要執行。這類作業一般稱為cron jobs。

1. 安裝crontab

yum -y install vixie-cron
yum -y install crontabs

說明:
vixie-cron 軟件包是 cron 的主程序;
crontabs 軟件包是用來安裝、卸裝、或列舉用來驅動 cron 守護進程的表格的程序。

 

2. 操作命令

cron 是 linux 的內置服務,但它不自動起來,可以用以下的方法啟動、關閉這個服務:

systemctl start   crond    #啟動服務
systemctl stop    crond    #關閉服務
systemctl restart crond    #重啟服務
systemctl reload  crond    #重新載入配置
systemctl status  crond    #查看crontab服務狀態
systemctl enable  crond    #開啟開機自動啟動
systemctl disable crond    #禁止開機自動啟動

 or

/sbin/service crond start    #啟動服務
/sbin/service crond stop     #關閉服務
/sbin/service crond restart  #重啟服務
/sbin/service crond reload   #重新載入配置
ntsysv                       #查看crontab服務是否已設置為開機啟動
chkconfig –level 35 crond on #開機自動啟動

 

 

3.cron配置

cron 的主配置文件是 /etc/crontab,它包括下面幾行:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  • 前四行是用來配置 cron 任務運行環境的變量。 SHELL 變量的值告訴系統要使用哪個 shell 環境(在這個例子里是 bash shell);
  • PATH 變量定義用來執行命令的路徑。
  • cron 任務的輸出被郵寄給 MAILTO 變量定義的用戶名。
  • 如果 MAILTO 變量被定義為空白字符串(MAILTO=""),電子郵件就不會被寄出。
  • HOME 變量可以用來設置在執行命令或腳本時使用的主目錄。

 

4.限制對 cron 的使用

/etc/cron.allow/etc/cron.deny 文件被用來限制對 cron 的使用。

  • 這兩個使用控制文件的格式都是每行一個用戶。 兩個文件都不允許空格。
  • 如果使用控制文件被修改了,cron 守護進程(crond)不必被重啟。
  • 使用控制文件在每次用戶添加或刪除一項 cron 任務時都會被讀取。
  • 無論使用控制文件中的規定如何,root 都總是可以使用 cron。
  • 如果 cron.allow 文件存在,只有其中列出的用戶才被允許使用 cron,並且 cron.deny 文件會被忽略。
  • 如果 cron.allow 文件不存在,所有在 cron.deny 中列出的用戶都被禁止使用 cron。

 

介紹crontab文件
        /etc/crontab
        在這個文件里並沒有記錄系統要執行哪些工作,而是記錄了下面四個子目錄。
        /etc/cron.hourly
        /etc/cron.daily
        /etc/cron.weekly
        /etc/cron.monthly
        這些子目錄里存放了一些腳本,到了crontab所指定的時間點,系統就會去執行這些子目錄里的腳本。 

 

 5. crontab 命令

 

5.1 功能

設置計時器。

 

5.2 語法

crontab[-u <用戶名稱>][配置文件]  
#or
crontab [-u <用戶名稱>][-elr]

cron 是一個常駐服務,它提供計時器的功能,讓用戶在特定的時間得以執行預設的指令或程序。只要用戶會編輯計時器的配置文件,就可以使 用計時器的功能。其配置文件格式如下:

 

Minute Hour Day Month DayOFWeek Command

 

參數說明:
-e 編輯該用戶的計時器設置,如果不指定用戶,則表示編輯當前用戶的crontab文件
-l 列出該用戶的計時器設置,如果不指定用戶,則表示顯示當前用戶的crontab文件內容。
-r 從/var/spool/cron目錄中刪除某個用戶的crontab文件,如果不指定用戶,則默認刪除當前用戶的crontab文件。

-i  在刪除用戶的crontab文件時給確認提示。
-u user 用來設定某個用戶的crontab服務,例如,“-u ixdba”表示設定ixdba用戶的crontab服務,此參數一般有root用戶來運行。
file file是命令文件的名字,表示將file做為crontab的任務列表文件並載入crontab。如果在命令行中沒有指定這個文件,crontab命令將接受標准輸入(鍵盤)上鍵入的命令,並將它們載入crontab。

5.3 格式

*   *  *  *  *  command
分 時 日 月 周  命令

第1列表示分鍾1~59 每分鍾用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要運行的命令

 

 

 

5.4 例子

 

30 21 * * * /usr/local/etc/rc.d/lighttpd restart       #每晚的21:30重啟apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart  #每月1、10、22日的4 : 45重啟apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart      #每周六、周日的1 : 10重啟apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart  #每天18 : 00至23 : 00之間每隔30分鍾重啟apache。
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart        #每星期六的11 : 00 pm重啟apache。
* */1 * * * /usr/local/etc/rc.d/lighttpd restart       #每一小時重啟apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart    #晚上11點到早上7點之間,每隔一小時重啟apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart  #每月的4號與每周一到周三的11點重啟apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart       #一月一號的4點重啟apache
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44           #每半小時同步一下時間

 

 6. set editor for crontab

for first edit of crontab -e, you will see the following contents:

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /usr/bin/emacs25
  5. /usr/bin/code
  6. /bin/ed

Choose 1-6 [1]: 

this asked us to select the editor for crontab, often, we use vim.tiny, so enter 3.

 

if we want to change the editor later, we can just type in select-editor in the shell terminal.

lenmom@M1701:~$ select-editor

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /usr/bin/emacs25
  5. /usr/bin/code
  6. /bin/ed

Choose 1-6 [1]: 

 

7. 編輯/etc/crontab 文件配置cron

cron服務每分鍾不僅要讀一次/var/spool/cron內的所有文件,還需要讀一次/etc/crontab,因此我們配置這個文件也能運用cron服務做一些事情。用crontab配置是針對某個用戶的,而編輯/etc/crontab是針對系統的任務。此文件的文件格式是:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root //如果出現錯誤,或者有數據輸出,數據作為郵件發給這個帳號
HOME=/      //使用者運行的路徑,這里是根目錄
# run-parts
01 * * * * root run-parts /etc/cron.hourly //每小時執行/etc/cron.hourly內的腳本
02 4 * * * root run-parts /etc/cron.daily //每天執行/etc/cron.daily內的腳本
22 4 * * 0 root run-parts /etc/cron.weekly //每星期執行/etc/cron.weekly內的腳本
42 4 1 * * root run-parts /etc/cron.monthly //每月去執行/etc/cron.monthly內的腳本

注意"run-parts"這個參數了,如果去掉這個參數的話,后面就可以寫要運行的某個腳本名,而不是文件夾名了

 

8.Reference

1. https://tool.lu/crontab/

2.  https://blog.csdn.net/gjggj/article/details/72922036

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM