0x1 cron簡介
cron是Linux系統中用於執行計劃任務的程序,利用cron可以實現在指定的時間週期性的執行某些任務。
0x2 crontab文件
cron執行的任務由crontab類型的配置文件指定,crontab文件的基本格式如下:
# 註釋
# 設置MAILTO則cron會把每條命令的輸出信息通過郵件發送給指定用戶
SHELL=指定運行命令的shell
PATH=指定環境變量PATH(注意此處不能通過$PATH調用默認環境變量,必須寫出所有路徑)
MAILTO=username
# 系統crontab文件任務項:分鐘 小時 天 月 星期 用戶名(只有系統crontab文件需要) 需要執行的命令
# 用戶crontab文件任務項:分鐘 小時 天 月 星期 需要執行的命令
# 每分鐘執行一次
* * * * * echo "hello cron"
# 每小時的第三十分鐘執行一次
30 * * * * echo "hello cron"
# 每天5時30分執行一次
30 5 * * * echo "hello cron"
# 每天5-7點中每小時的第30分鐘執行一次
10 5-7 * * * echo "hello cron"
以#
開頭的爲註釋,每一項定時任務佔用一行,共有6個字段(系統crontab文件有7個字段,多了一個用戶名字段),使用空格分隔每個字段。其中前面5個字段則是該命令執行的時間,最後一個字段是需要執行的命令。
時間字段
命令執行時間的取值範圍如下:
時間類型 | 取值範圍 |
---|---|
分鐘 | 0-59 |
小時 | 0-23 |
天 | 1-31 |
月 | 1-12 |
星期 | 0-7(0和7都表示星期日) |
範圍時間表示:
符號 | 表示內容 |
---|---|
* | 所有時間,某個時間字段設置爲*表示從最小值到最大值的範圍 |
a-b | a到b範圍內的所有時間,如月份中1-12表示從1月到12月每月執行一次 |
a-b/c | a-b範圍內間隔爲c的所有時間,如月份中1-12/2表示從1月到12月每隔2月執行一次 |
特殊時間表示:
可以用如下表示方式代替前5個字段來指定指令執行時間
符號 | 表示內容 |
---|---|
@reboot | 啓動時執行 |
@yearly | 每年一次 |
@annually | 每年一次 |
@monthly | 每月一次 |
@weekly | 每週一次 |
@daily | 每天一次 |
@hourly | 每小時一次 |
命令字段
命令字段可以是任意有效命令,並且第一個非轉義(使用\
轉義)的%
後邊的內容會作爲該命令的標準輸入(stdin),並且所有非轉義的%
會被作爲換行符處理(在標準輸入內容中需要換行時就可以使用%
來表示)
* * * * * read var % hello this is input content
訪問crontab 5獲取關於crontab文件的格式更詳細的描述或者在linux終端中鍵入man 5 crontab來獲取
0x3 創建和修改定時任務的方法
編輯系統crontab文件
第一種方法是使用文本編輯器直接修改系統crontab文件(main system crontab file)。系統crontab文件路徑爲/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 )
使用crontab命令
第二種方式是修改/var/spool/cron/<username>用戶crontab文件,一般通過crontab命令來操作該文件,流程如下:
首先在任意位置創建用戶crontab文件,例如建立名爲my_crontab文件。
# min hour day month week
MAILTO=january
* * * * * echo "hello crontab"
然後使用crontab命令設置該文件爲用戶crontab文件,crontab命令會把指定文件內容複製到/var/spool/cron/<username>文件中
crontab my_crontab
使用crontab -l
命令來查看/var/spool/cron/<username>用戶crontab文件
使用crontab -r
命令來刪除/var/spool/cron/<username>用戶crontab文件,從而停止執行定時任務
使用crontab -e
命令來直接編輯/var/spool/cron/<username>用戶crontab文件,從而修改定時任務
訪問crontab 1獲取關於crontab命令更詳細的描述,或者在linux終端中鍵入man crontab來獲取。
關於MAILTO
指定了MAILTO選項並且在安裝了本地郵件系統(postfix,sendmail)時,cron程序會把執行命令的輸出內容通過郵件發送給MAILTO指定的用戶,下面是cron程序發送的郵件:
Return-Path: <[email protected]>
X-Original-To: january
Delivered-To: [email protected]
Received: by january-PC (Postfix, from userid 1000)
id B38592801EB; Wed, 17 Jul 2019 10:40:01 +0800 (CST)
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <january@january-PC> echo "hello crontab"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <MAILTO=january>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/january>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=january>
Message-Id: <20190717024001.B38592801EB@january-PC>
Date: Wed, 17 Jul 2019 10:40:01 +0800 (CST)
X-IMAPbase: 1563331245 5
Status: O
X-UID: 4
hello crontab
最後的hello crontab是命令輸出內容。
本地用戶的郵件會放置在/var/mail/<username>
文件中,可以安裝mailx
軟件來管理和查看
關於運行日誌
對於安裝了rsyslog
的系統,在/var/log/
目錄中會有cron的運行日誌,沒有rsyslog
的系統可以使用journalctl
命令查看日誌,鍵入journalctl -u cron.service
或者journalctl | grep cron
即可。