[TOC]
1. Linux有個三種方式 **/etc/cron.d** , **/etc/crontab文件**和**crontab -e**,遇到的很多黑客攻擊都會用定時來不斷的摧殘我們的系統。
2. cron服務的最低檢測時間單位是分鐘,所以cron會每分鐘讀取一次/etc/crontab與/var/spool/cron中的數據內容,因此,編輯完/etc/crontab文件并且保存之后,crontab時設定就會自動執行。
**3. cron執行時,也就是要讀取三個地方的配置文件:一是/etc/crontab,二是/etc/cron.d目錄下的所有文件,三是每個用戶的配置文件. **
## **1. /etc/crontab文件**
### **1.1這種設置定時任務的方法,只限于root用戶(用于root給自己與其他用戶分配定時任務)**
```
root@ubuntu01:~# ll /etc/crontab
-rw-r--r-- 1 root root 420 Nov 8 18:25 /etc/crontab
```
如上,只有root有權修改這個文件。
### **1.2創建定時任務**
```
* * * * * <用戶> <commond> :給某一用戶創建定時任務
```
```
root@ubuntu01:~# vim /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
*/2 * * * * root echo "dd" >> /root/test
```
### **1.3.這種方式,通過`crontab -l`是*不能顯示用戶的定時任務的***
```
root@ubuntu01:~# crontab -l
no crontab for root
```
### **1.4直接刪除定時任務的那行就可以停止定時任務**
### **1.5 centos與Ubuntu的區別**
#### 1.5.1 Ubuntu的/etc/crontab
```
root@ubuntu02:~# vim /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 )
```
#### 1.5.2 centos的 /etc/crontab
```
[root@localhost ~]# vi /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
```
### 1.5.3說明
1.Ubuntu的文件多出了幾行`25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )`這種東西
2.run-parts --report /etc/cron.daily ://每天執行/etc/cron.hourly內的腳本,run-parts代表執行目錄中的腳本,不加的話要指定執行的腳本了,而不是目錄。
3.難道centos就沒有類似的這種文件嗎?有:
```
[root@localhost etc]# ls | grep cron
anacrontab
cron.d
cron.daily
cron.deny
cron.hourly
cron.monthly
crontab
cron.weekly
```
## **2.crontab -e**
**1.這種所有用戶都可以使用,普通用戶也只能為自己設置計劃任務
2.然后自動將定時任務寫入/var/spool/cron/crontabs/username**
### **2.1創建定時任務**
```
tuna@ubuntu01:~$ crontab -e
# Edit this file to introduce tasks to be run by cron.
...
#
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * echo "I am tuna,hello!" > ~/tuna.txt
```
### **2.2查看定時任務**
```
tuna@ubuntu01:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
...
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * echo "I am tuna,hello!" > ~/tuna.txt
```
### **2.3刪除定時任務**
執行下邊的命令后,自動刪除屬于當前用戶的定時任務
```
crontab -r
```
## 3. /etc/cron.d
```
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # 發生錯誤會給/var/spool/mail/root發送email
*/1 * * * * root /root/test.sh
```
## 4. crontab的限制
```
/etc/cron.allow:將可以使用 crontab 的帳號寫入其中,若不在這個文件內的使用者則不可使用 crontab;
/etc/cron.deny:將不可以使用 crontab 的帳號寫入其中,若未記錄到這個文件當中的使用者,就可以使用 crontab 。
```
如果有需要,使用其中一種方式即可
### 3.1 實例
```
root@ubuntu01:~# cat /etc/cron.deny
tuna test
```
## **5.anacron**
1.anacron并不能取代cron去運行某項任務,而是以天為單位或者是在啟動后立刻進行anacron的動作,**它會去偵測停機期間應該進行但是并沒有進行的crontab任務,并將該任務運行一遍**后,anacron就會自動停止了。
**2.anacron運行的時間通常由兩個,一個是系統啟動期間運行,一個是寫入crontab的排程中,這樣才能夠在特定時間分析系統未進行的crontab工作。**
**3.anacron的配置文件是/etc/anacrontab**
```
[root@localhost cron.hourly]# vi /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
```
**4.anacron命令的語法如下:**
(1)-s開始連續的運行各項工作,會一句時間記錄當的數據判斷是否進行。
(2)-f強制進行,而不去判斷時間登錄檔的時間戳。
(3)-n立即進行未進行的任務,而不延遲等待時間。
(4)-u僅升級時間記錄當的時間戳,不進行任何工作。
而anacron的配置文件是/etc/anacrontab,而它的很多內容則是在/var/spool/anacron里面保存。
**5.當anacron下達anacron -s cron.daily時,它會有如下的步驟:**
(1)由/etc/anacrontab分析到cron.daily這項工作名稱的天數為一天。
(2)由/var/spool/anacron/cron.daily取出最近一次運行anacron的時間戳。
(3)把取出的時間戳與當前的時間戳相比較,如果差異超過了一天,那么就準備進行命令。
(4)若準備進行命令,根據/etc/anacrontab的配置,將延遲65分鐘。
(5)延遲時間后,開始運行后續命令,也就是run-parts /etc/cron.daily這串命令。
(6)運行完畢后,anacron程序結束。
**6. crontab調用anacron**
1)/etc/cron.d/0hourly
/etc/cron.d目錄
cron進程執行時,就會自動掃描該目錄下的所有文件,按照文件中的時間設定執行后面的命令。
```
[root@localhost cron.d]# vi 0hourly
#nacron Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
```
2)/etc/cron.hourly
```
[root@localhost cron.hourly]# ls
0anacron
[root@localhost cron.hourly]# vi 0anacron
#!/bin/sh
# Check whether 0anacron was run today already
if test -r /var/spool/anacron/cron.daily; then
day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi
# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power >/dev/null 2>&1
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron -s //調用
```
如上腳本所示,會按照天去檢查是否有未執行的定時任務
- Linux
- 高級
- 殺毒
- 記一次中毒事件
- clamav查毒軟件
- 處理挖礦病毒
- 定時任務
- kill
- chattr文件保護
- 運行級別
- Linux啟動
- 文件加密
- ssh免密登錄
- .ssh
- 問題
- 腳本
- 阿里云域名解析
- yum源
- 時間同步
- keepalived實現高可用
- dos字符與unix字符
- 大文件上傳
- 基礎
- proc目錄
- 設置宋體
- 基礎命令_01
- 基礎命令_02
- SELinux
- 文件描述符
- 基礎命令_03
- awk
- 系統日志
- date命令
- bc命令
- lsof
- vim快捷鍵
- shell
- 循環控制
- expr
- 執行腳本的方式
- declare
- shell腳本
- 控制啟停腳本
- 數值計算
- centos
- 配置網絡
- 環境
- 灰度環境
- ansible
- 模塊
- 語法
- file模塊
- setup模塊
- ping模塊
- copy模塊
- command模塊
- shell模塊
- service模塊
- cron模塊
- yum模塊
- user 模塊
- group模塊
- 指定用戶
- playbook
- 實例
- ansible安裝
- Jenkins
- shell部署
- 導入已有項目的配置
- 執行shell
- tungsten數據同步
- 防火墻
- netfilter