crontab 命令使用

xuyaowen發表於2018-06-25

什麼是crontab?

      crontab命令常見於Unix和類Unix的作業系統之中,用於設定週期性被執行的指令。該命令從標準輸入裝置讀取指令,並將其存放於“crontab”檔案中,以供之後讀取和執行。該詞來源於希臘語 chronos(χρνο),原意是時間。通常,crontab儲存的指令被守護程式啟用, crond常常在後臺執行,每一分鐘檢查是否有預定的作業需要執行。這類作業一般稱為cron jobs。

怎麼使用?

1. 安裝 

   如果沒有安裝,就進行安裝。

➜  Downloads rpm -qa | grep cron
crontabs-1.11-16.20150630git.fc28.noarch
cronie-1.5.1-9.fc28.x86_64
cronie-anacron-1.5.1-9.fc28.x86_64 

2. 服務啟動和關閉

使用 systemctl 管理

➜  Downloads systemctl restart crond.service 

3. 配置檔案

➜  ~ ls -l /etc/cron*
-rw-r--r--. 1 root root    0 Feb  7 13:58 /etc/cron.deny
-rw-r--r--. 1 root root  451 Feb  7 13:57 /etc/crontab

/etc/cron.d:
total 8
-rw-r--r--. 1 root root 128 Feb  7 13:58 0hourly
-rw-r--r--. 1 root root 108 Aug  4  2017 raid-check

/etc/cron.daily:
total 24
-rwxr-xr-x. 1 root root 18812 Apr 26 10:21 google-chrome
-rwxr-xr-x. 1 root root   189 Jan  4 19:58 logrotate

/etc/cron.hourly:
total 4
-rwxr-xr-x. 1 root root 575 Feb  7 13:58 0anacron

/etc/cron.monthly:
total 0

/etc/cron.weekly:
total 4
-rwxr-xr-x. 1 root root 603 Aug  5  2017 98-zfs-fuse-scrub
  • cron.daily是每天執行一次的任務
  • cron.weekly是每個星期執行一次的任務
  • cron.monthly是每月執行一次的任務
  • cron.hourly是每個小時執行一次的任務
  • cron.d是系統自動定期需要做的任務
  • crontab是設定定時任務執行檔案
  • cron.deny檔案就是用於控制不讓哪些使用者使用crontab的功能

每個使用者都有自己的cron配置檔案,通過crontab -e 就可以編輯,一般情況下我們編輯好使用者的cron配置檔案儲存退出後,系統會自動就存放於/var/spool/cron/目錄中,檔案以使用者名稱命名.linux的cron服務是每隔一分鐘去讀取一次/var/spool/cron,/etc/crontab,/etc/cron.d下面所有的內容.

➜  ~ sudo ls -l /var/spool/cron /etc/crontab /etc/cron.d
[sudo] password for xuyaowen: 
-rw-r--r--. 1 root root  451 Feb  7 13:57 /etc/crontab

/etc/cron.d:
total 8
-rw-r--r--. 1 root root 128 Feb  7 13:58 0hourly
-rw-r--r--. 1 root root 108 Aug  4  2017 raid-check

/var/spool/cron:
total 0

4. 配置檔案格式

  *           *        *        *        *           command

minute   hour    day   month   week      command

分          時         天      月        星期       命令

 

星號(*):代表所有可能的值,例如month欄位如果是星號,則表示在滿足其它欄位的制約條件後每月都執行該命令操作。

逗號(,):可以用逗號隔開的值指定一個列表範圍,例如,“1,2,5,7,8,9”。

中槓(-):可以用整數之間的中槓表示一個整數範圍,例如“2-6”表示“2,3,4,5,6”。

正斜線(/):可以用正斜線指定時間的間隔頻率,例如“0-23/2”表示每兩小時執行一次。同時正斜線可以和星號一起使用,例如*/10,如果用在minute欄位,表示每十分鐘執行一次。

5. crontab 命令使用方法

可以通過 man 命令檢視具體的使用的方法

 crontab [-u user] file
 crontab [-u user] [-l | -r | -e] [-i] [-s]
 crontab -n [ hostname ]
 crontab -c

-u  Appends the name of the user whose crontab is to be modified. If this option is not used, crontab examines “your” crontab, i.e., the crontab of the
person executing the command. Note that su(8) may confuse crontab, thus, when executing commands under su(8) you should always use the -u option.
If no crontab exists for a particular user, it is created for him the first time the crontab -u command is used under his username.

-l  Displays the current crontab on standard output.

-r  Removes the current crontab.

-e  Edits 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.

-i  This option modifies the -r option to prompt the user for a `y/Y` response before actually removing the crontab.

-s  Appends the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing / replacement occurs – see the docu‐
mentation of MLS_LEVEL in crontab(5).

-n  This option is relevant only if cron(8) was started with the -c option, to enable clustering support. It is used to set the host in the cluster
which should run the jobs specified in the crontab files in the /var/spool/cron directory. If a hostname is supplied, the host whose hostname
returned by gethostname(2) matches the supplied hostname, will be selected to run the selected cron jobs subsequently. If there is no host in the
cluster matching the supplied hostname, or you explicitly specify an empty hostname, then the selected jobs will not be run at all. If the hostname
is omitted, the name of the local host returned by gethostname(2) is used. Using this option has no effect on the /etc/crontab file and the files in
the /etc/cron.d directory, which are always run, and considered host-specific. For more information on clustering support, see cron(8).

-c  This option is only relevant if cron(8) was started with the -c option, to enable clustering support. It is used to query which host in the cluster
is currently set to run the jobs specified in the crontab files in the directory /var/spool/cron , as set using the -n option.

6. 命令示例

8,12,16 * * * /data/app/scripts/monitor/df.sh
2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh
8,12,16 * * * /data/app/scripts/monitor/check_ind_unusable.sh
8,12,16 * * * /data/app/scripts/monitor/check_maxfilesize.sh
8,12,16 * * * /data/app/scripts/monitor/check_objectsize.sh
21 * * *  每天的21:43 執行
05 * * *    每天的05:15 執行
17 * * * 每天的17:00 執行
17 * * 1 每週一的 17:00 執行

0,10 17 * * 0,2,3 每週日,週二,週三的 17:00和 17:10 執行

0-10 17 1 * * 毎月1日從 17:00到7:10 毎隔1分鐘 執行
0 1,15 * 1 毎月1日和 15日和 一日的 0:00 執行
4 1 * *     毎月1日的 4:42分 執行
21 * * 1-6   週一到週六 21:00 執行

0,10,20,30,40,50 * * * * 每隔10分 執行

*/10 * * * *        每隔10分 執行

* 1 * * *         從1:0到1:59 每隔1分鐘 執行
1 * * *         1:00 執行
*/1 * * *        毎時0分 每隔1小時 執行
* * * *         毎時0分 每隔1小時 執行
8-20/3 * * *      8:02,11:02,14:02,17:02,20:02 執行
5 1,15 * *       1日 和 15日的 5:30 執行
 
7. 配合 notify-send 進行通知
➜  ~ crontab -l
0-5/1 9 * * * notify-send "吃早飯了,boy!"
0-5/1 11 * * * notify-send "吃水果了,boy!"
0-5/1 13 * * * notify-send "吃午飯了,boy!"
0-5/1 15 * * * notify-send "吃下午茶了,boy!"
0-5/1 18 * * * notify-send "下班了,boy!"
保持更新,轉載請表明出處。
 

相關文章