- crontab:通過指令制定計(jì)劃任務(wù)。讓系統(tǒng)自動(dòng)進(jìn)行預(yù)設(shè)的任務(wù)。
- 4-12 內(nèi)容:
學(xué)習(xí) crontab 命令創(chuàng)建計(jì)劃任務(wù)。
1、 cron:是一個(gè)線程,它可以讓 linux 周期性的執(zhí)行某一命令。
2、linux 指定計(jì)劃任務(wù)有兩種方式。一種是通過 crontab 命令、另一種是通過 /etc/crontab 配置文件。下面先操作 crontab 命令,后操作 /etc/crontab 文件。
3、 crontab:是一個(gè)命令,可以設(shè)置 linux 周期性執(zhí)行某一命令。
crontab 參數(shù)選項(xiàng):
-u:設(shè)置某一用戶的周期性工作(只有 root 權(quán)限可以設(shè)置)。
-e:編輯 crontab 的工作內(nèi)容。
-l:查閱 crontab 的工作內(nèi)容。
-r:移除 crontab 的所有工作內(nèi)容,如需移除單項(xiàng)或非全部,可用 -e 編輯。
4、 crontab 的格式概括:
crontab 命令的格式:* * * * * command。
crontab 由 6 項(xiàng)參數(shù)組成,它們是:minute、hour、day、month、week、command。前面 5 個(gè) * 星號分別代表 minute、hour、day、month、week,command 為執(zhí)行的命令。
minute:表示分鐘,0 到 59 之間的任何整數(shù)。
hour:表示小時(shí),0 到 23 之間的任何整數(shù)。
day:表示日期,1 到 31 之間的任何整數(shù)。
month:表示月份,1 到 12 之間的任何整數(shù)。
week:表示星期幾,0 到 7 之間的任何整數(shù),0 和 7代表星期日。
command:需要執(zhí)行的命令,可以是系統(tǒng)命令,也可以是腳本文件。
特殊符號(* 星號、,逗號,- 減號,/ 斜杠),特定的符號可以組合參數(shù)的格式、表示范圍 和 時(shí)間周期。符號格式是表達(dá)式的一種,5 個(gè) * 號對標(biāo)參數(shù)格式圖解如下:

5、特殊符號解析:
-
*:星號,代表每個(gè)時(shí)刻。如 5 個(gè)星號就是每 1 分鐘,每 1 個(gè)小時(shí),每 1 天,每 1 個(gè)月,每 1 周。簡單理解就是每時(shí)每刻,每1分鐘執(zhí)行一次命令。
* 號圖解 -
,:逗號,代表分隔字段。例如:15,20,45 * * * * command 代表每小時(shí)的第 15分、20分、45分執(zhí)行命令。
,逗號圖解 -
-:減號,代表一段時(shí)間的范圍。例如:10 7-10 * * * command 代表每天的 7點(diǎn) 到 10點(diǎn)這個(gè)時(shí)間范圍,逢第 10 分鐘執(zhí)行命令。也就是 7點(diǎn)10分、8點(diǎn)10分、9點(diǎn)10分、10點(diǎn)10分執(zhí)行命令。
,逗號 - 減號圖解 -
/n:/ 代表間隔,n 代表數(shù)字,也就是每隔 n 個(gè)單位間隔的意思。例如:*/5 * * * * command 代表每隔 5 分鐘執(zhí)行命令一次。
/ 斜杠圖解
6、crontab 命令案例實(shí)操:
- 1)、root 用戶對普通用戶進(jìn)行計(jì)劃作業(yè)操作。
- 虛擬機(jī)登錄兩個(gè)用戶,一個(gè)是 root 用戶,一個(gè)是 torres 普通用戶。root 用戶可以自身制定 crontab 計(jì)劃作業(yè),也可對普通用戶制定 crontab 計(jì)劃作業(yè)。普通用戶只能對自身制定 crontab 計(jì)劃作業(yè)。不能對自身以外的用戶制定 crontab。

-
①、torres 用戶 home 目錄下現(xiàn)在沒有文件。
torres 用戶 ②、root 用戶執(zhí)行 crontab -eu torres ,對 torres 用戶制定計(jì)劃。
[root@localhost ~]# crontab -eu torres ## 對 torres 用戶制定計(jì)劃
no crontab for torres - using an empty one
* * * * * date >> mydate.txt ## 每分鐘輸出 date 到 mydate.txt 文件
~
~
~
:wq
-
③、root 用戶可用 -lu 參數(shù)查看指定用戶的 crontab 計(jì)劃任務(wù),普通用戶 torres 也可以用 -l 參數(shù)查看自身正在執(zhí)行的 crontab 計(jì)劃任務(wù)(-l -u 查看指定用戶計(jì)劃任務(wù),-l 查看自身計(jì)劃任務(wù))。如 root 對 torres 用戶制定了計(jì)劃,root 可以查看,torres 也可以查看 。
root 通過 -lu 查看 torres 用戶的 crontab 計(jì)劃作業(yè)

- ④、root 對 torres 指定的 crontab 任務(wù)開始后,只要到了指令預(yù)設(shè)的時(shí)間,mydate.txt 文件就會生成在 torres 家目錄下,并一直存儲數(shù)據(jù)。如,root 指定的 crontab 指令是 * * * * * date >> mydate.txt,每分鐘輸出 date 到 mydate.txt 文件。開始執(zhí)行后 1 分鐘就會生成 mydate.txt 文件在 torres 的家目錄下,并且一直存儲每一分鐘 date 輸出的數(shù)據(jù)。

- ⑤、root 可以修改普通用戶的 crontab,torres 也可以修改自身的 crontab。
## root 修改普通用戶的 crontab
[root@localhost ~]#
[root@localhost ~]# crontab -lu torres ## root 通過 -lu 查看 torres 用戶的 crontab
* * * * * date >> mydate.txt
[root@localhost ~]#
[root@localhost ~]# crontab -eu torres root 通過 -eu 編輯 torres 用戶的 crontab
* * * * * date >> mydate.txt
## 新增一條每 2 分鐘輸出 i am root 到 root_echo.txt
*/2 * * * * echo "i am root" >> root_echo.txt
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## 完成對 torres 的 crontab 設(shè)置
[root@localhost ~]#
[root@localhost ~]# crontab -lu torres ## 現(xiàn)在 torres 用戶的 crontab 有兩條
* * * * * date >> mydate.txt
*/2 * * * * echo "i am root" >> root_echo.txt
[root@localhost ~]#
- crontab 指令預(yù)設(shè)的時(shí)間一到就會生成 root_echo.txt 文件在 torres 家目錄下。
[torres@localhost ~]$
[torres@localhost ~]$ ll
total 8
-rw-r--r--. 1 torres torres 812 Sep 28 21:45 mydate.txt
-rw-r--r--. 1 torres torres 50 Sep 28 21:44 root_echo.txt
[torres@localhost ~]$
[torres@localhost ~]$ cat root_echo.txt ## 查看 root_echo.txt 文件內(nèi)容
i am root
i am root
i am root
i am root
i am root
[torres@localhost ~]$
- ⑥、普通用戶 torres 也可以編輯自身正在運(yùn)行的 crontab
[torres@localhost ~]$
[torres@localhost ~]$ crontab -e ## torres 編輯自身運(yùn)行的 crontab 只需 -e
* * * * * date >> mydate.txt
*/2 * * * * echo "i am root" >> root_echo.txt
## 新增一條每 2 分鐘輸出 i am torres 到 torres_echo.txt
*/2 * * * * echo "i am torres" >> torres_echo.txt
~
~
~
:wq ## 保存并退出
- crontab 指令預(yù)設(shè)的時(shí)間一到就會生成 torres_echo.txt 文件在 torres 家目錄下。
crontab: installing new crontab
[torres@localhost ~]$
[torres@localhost ~]$ ll
total 12
-rw-r--r--. 1 torres torres 1131 Sep 28 21:56 mydate.txt
-rw-r--r--. 1 torres torres 110 Sep 28 21:56 root_echo.txt
-rw-r--r--. 1 torres torres 36 Sep 28 21:56 torres_echo.txt
[torres@localhost ~]$
[torres@localhost ~]$ cat torres_echo.txt ## 查看 torres_echo.txt 文件內(nèi)容
i am torres
i am torres
i am torres
[torres@localhost ~]$
-
⑦、注意,root 對普通用戶執(zhí)行 crontab 時(shí),指定用戶的 -u 參數(shù)一定要放在最后,如:編輯時(shí) -eu、查看時(shí) -lu、刪除時(shí) -ru。如果位置互換了,則會報(bào)錯(cuò)。
root 編輯指定用戶的 crontab 時(shí),-u 參數(shù)應(yīng)放最后,否則會報(bào)錯(cuò) - ⑧、普通用戶只能對自身制定 crontab 計(jì)劃作業(yè)。不能對自身以外的用戶制定crontab。
由 root 創(chuàng)建的用戶除了 torres,還有 user1 和 x 用戶。
[root@localhost ~]#
[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
torres:x:1000:1007::/home/torres:/bin/bash ## <------------普通用戶
user1:x:1001:1008::/home/user1:/bin/bash ## <------------普通用戶
x:x:1002:1009::/home/x:/bin/bash ## <------------普通用戶
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
[root@localhost ~]#
- 用 torres 用戶為 user1 用戶創(chuàng)建計(jì)劃作業(yè),會提示沒有 -u 的特權(quán)。
[torres@localhost ~]$
[torres@localhost ~]$ crontab -eu user1
must be privileged to use -u ## 必須有使用-u的特權(quán)
[torres@localhost ~]$
- 總結(jié):
每個(gè)用戶都可以通過 crontab -e 的命令制定計(jì)劃任務(wù)。root 用戶權(quán)限大,除了用 -e、-l、-r 為自身可以進(jìn)行創(chuàng)建(編輯)、查看、刪除計(jì)劃任務(wù)外,還可以通過 -eu、-lu、-ru 來為指定的普通用戶創(chuàng)建(編輯)、查看、刪除計(jì)劃任務(wù)。而普通用戶只能通過 -e、-l、-r 為自身可以進(jìn)行創(chuàng)建(編輯)、查看、刪除計(jì)劃任務(wù),沒有權(quán)限通過 -eu、-lu、-ru 對自身以外的用戶進(jìn)行創(chuàng)建(編輯)、查看、刪除計(jì)劃任務(wù)的操作。
以上是 root 用戶為普通用戶創(chuàng)建、修改 crontab 任務(wù)。下面用 root 用戶做一些 crontab 命令實(shí)操,了解不同時(shí)間的輸出情況:
2)、每 1 分鐘輸出一次 date 系統(tǒng)時(shí)間,保存到當(dāng)前目錄 date.txt 文件中,如果當(dāng)前目錄沒有 date.txt 文件會自動(dòng)創(chuàng)建一個(gè)。
## 當(dāng)前用戶創(chuàng)建 crontab 任務(wù),-e 編輯模式,vim 界面。
[root@localhost ~]# crontab -e
## 按 I 進(jìn)入插入模式進(jìn)行編輯
## 輸入指令,五個(gè) * 代表每 1分鐘執(zhí)行一次指令。
* * * * * date >> date.txt
~
~
~
~
:wq ## wq 保存退出
[root@localhost ~]#
[root@localhost ~]# ll ## 此時(shí),ll 查看當(dāng)前目錄已經(jīng)有 date.txt 文件了,大小為 58 字節(jié)
total 4
-rw-r--r--. 1 root root 58 Sep 26 00:08 date.txt
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# date ## 再過了一會,查看當(dāng)前時(shí)間。
Sun Sep 26 00:11:54 CST 2021
[root@localhost ~]#
[root@localhost ~]# ll ## 此時(shí),date.txt 文件大小為 145 字節(jié)
total 4
-rw-r--r--. 1 root root 145 Sep 26 00:11 date.txt
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# cat date.txt ## cat 查看 date.txt 目錄的內(nèi)容。從輸出的時(shí)間看出是每 1 分鐘輸出一次
Sun Sep 26 00:07:01 CST 2021
Sun Sep 26 00:08:01 CST 2021
Sun Sep 26 00:09:01 CST 2021
Sun Sep 26 00:10:01 CST 2021
Sun Sep 26 00:11:02 CST 2021
[root@localhost ~]#
[root@localhost ~]# crontab -l ## -l 列出當(dāng)前 crontab 的任務(wù)內(nèi)容
* * * * * date >> date.txt
[root@localhost ~]#
[root@localhost ~]# crontab -r ## -r 刪除當(dāng)前 crontab 的任務(wù)內(nèi)容
[root@localhost ~]#
[root@localhost ~]# crontab -l ## 當(dāng) -r 刪除了任務(wù)內(nèi)容后,再用 -l 列出內(nèi)容,會提示 root 用戶沒有 crontab 任務(wù)了。
no crontab for root
[root@localhost ~]#
3)、* * * * * date >> date.txt 是每 1 一分鐘執(zhí)行一次命令,還有第二種寫法是 */1 * * * * date >> date.txt。/ 代表 “每” 的意思。在第一個(gè) * 號區(qū)間用 /n 的寫法,代表每 n 分鐘執(zhí)行一次命令。同理,在其他 * 號區(qū)間用 /n 的寫法,代表每 n 小時(shí)、每 n 天、每 n 月、每 n 周執(zhí)行一次命令。
假設(shè),現(xiàn)在需要每 5 分鐘輸出一次 date 系統(tǒng)時(shí)間,保存到 tmp 目錄 5minute-date.txt 文件中(如果 tmp 目錄沒有 5minute-date.txt 文件會自動(dòng)創(chuàng)建一個(gè))。這時(shí)可以用 /5 的寫法完成。
## 當(dāng)前用戶創(chuàng)建 crontab 任務(wù),-e 編輯模式,vim 界面。
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
## 按 I 進(jìn)入插入模式進(jìn)行編輯
## 輸入指令,*/5 * * * *代表每 5 分鐘執(zhí)行一次指令。
*/5 * * * * date >> /tmp/5minute-date.txt
~
~
~
:wq ## wq 保存退出
[root@localhost ~]#
[root@localhost ~]# ll /tmp/ ## 已經(jīng)有 5minute-date.txt , 最后修改時(shí)間 00:50,大小 87 字節(jié)
total 8
-rw-r--r--. 1 root root 87 Sep 26 00:50 5minute-date.txt ## crontab 產(chǎn)生的文件
[root@localhost ~]#
[root@localhost ~]# cat /tmp/5minute-date.txt ## cat 查看文件內(nèi)容
Sun Sep 26 00:40:01 CST 2021
Sun Sep 26 00:45:01 CST 2021
Sun Sep 26 00:50:01 CST 2021
[root@localhost ~]#
[root@localhost ~]# crontab -r ## 刪除當(dāng)前用戶(root)所有的 crontab 任務(wù)
[root@localhost ~]#
[root@localhost ~]# crontab -l ## 當(dāng)前用戶(root)沒有 crontab 任務(wù)
no crontab for root
[root@localhost ~]#
綜上測試, / 斜杠是 “每” 的意思,每多少分鐘,每多少小時(shí),每多少天,每多少月,每多少周,都是通過 / 后面跟的合法數(shù)值來表示。
另外,通過測試可以看出,crontab 執(zhí)行任務(wù)的時(shí)間是到點(diǎn)后 1 秒開始執(zhí)行命令。如:00:40:01、00:45:01、 00:50:01。
4)、以上的寫法是通過執(zhí)行 crontab 命令,然后進(jìn)入編輯模式進(jìn)行命令的編輯。簡單的指令可以這樣做,對于復(fù)雜或者比較多命令時(shí)會顯得不太好管理。另一種方法,通過編寫腳本,crontab 讀取腳本的形式執(zhí)行計(jì)劃作業(yè)。
例:現(xiàn)在需要用腳本的寫法完成每 2 分鐘輸出一次 date 系統(tǒng)時(shí)間,保存到 tmp 目錄 2minute.txt 文件中,如果 tmp 目錄沒有 2minute.txt 文件會自動(dòng)創(chuàng)建一個(gè)。
[root@localhost ~]# pwd ## 由于是在當(dāng)前目錄下創(chuàng)建腳本文件,所以養(yǎng)成習(xí)慣查看一下當(dāng)前路徑
/root
[root@localhost ~]#
[root@localhost ~]# vim mydate.sh ## vim 編輯腳本文件,名為 mydate.sh
## 腳本的編寫是執(zhí)行的內(nèi)容
date >> /tmp/2minute.txt ## 內(nèi)容是 date 輸出時(shí)間到 /tmp/2minute.txt 文件
~
~
~
:wq ## 保存并退出
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 174 Sep 26 00:12 date.txt
-rw-r--r--. 1 root root 25 Sep 26 01:52 mydate.sh ## 腳本文件
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]# chmod u+x mydate.sh ## 賦予腳本文件擁有者具有 x 執(zhí)行權(quán)限
[root@localhost ~]#
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 174 Sep 26 00:12 date.txt
-rwxr--r--. 1 root root 25 Sep 26 01:52 mydate.sh ## 賦有 x 執(zhí)行權(quán)限的腳本文件
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]# crontab -e ## 當(dāng)前用戶創(chuàng)建 crontab 任務(wù)
no crontab for root - using an empty one
*/2 * * * * /root/mydate.sh ## crontab 任務(wù)的編寫是控制時(shí)間 和 對象
~
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# date ## 查看當(dāng)前時(shí)間,距離執(zhí)行計(jì)劃作業(yè)已有一段時(shí)間
Sun Sep 26 02:03:59 CST 2021
[root@localhost ~]#
[root@localhost ~]# ll /tmp/ ## 查看 tmp 目錄,已有 2minute.txt 的文件,116 字節(jié),最后修改時(shí)間 02:04
total 8
-rw-r--r--. 1 root root 116 Sep 26 02:04 2minute.txt
[root@localhost ~]#
[root@localhost ~]# cat /tmp/2minute.txt ## 查看 2minute.txt 文件,已有記錄
Sun Sep 26 01:58:01 CST 2021
Sun Sep 26 02:00:01 CST 2021
Sun Sep 26 02:02:01 CST 2021
Sun Sep 26 02:04:01 CST 2021
[root@localhost ~]#
- 5)、格式錯(cuò)誤及相關(guān)注意事項(xiàng):
需要注意的是用 / 斜杠 的區(qū)間前面的 * 號不能去除。如果去除 * 號會提示命令格式錯(cuò)誤,要求重新編輯。
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
/3 * * * * date >> 3minute.txt ## 當(dāng) /3 前沒有 * 號時(shí),保存文檔
~
~
~
:wq ## 保存并退出
crontab: installing new crontab
"/tmp/crontab.vzxojY":1: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? ## 系統(tǒng)會提示出錯(cuò),并問是否進(jìn)行修改
Do you want to retry the same edit? y ## 輸入 y,進(jìn)行修改。n 退出 crontab 編輯
*/3 * * * * date >> 3minute.txt ## 輸入 y,/3 前加上 * 號可正常保存退出
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## 新的 crontab 計(jì)劃創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# ll ## 等待一段時(shí)間,已經(jīng)有 3minute.txt 的文檔,大小 87 字節(jié),最后修改時(shí)間 08:51
total 4
-rw-r--r--. 1 root root 87 Sep 26 08:51 3minute.txt
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# cat 3minute.txt ## 列出 3minute.txt 文件內(nèi)容,已有數(shù)據(jù)
Sun Sep 26 08:45:01 CST 2021
Sun Sep 26 08:48:01 CST 2021
Sun Sep 26 08:51:01 CST 2021
[root@localhost ~]#
- 其他格式錯(cuò)誤也會提示是否重新編輯,當(dāng)提示是否重新編輯的時(shí)候,輸入 n 退出 crontab 的創(chuàng)建。如需創(chuàng)建 crontab 要重新 crontab -e。輸入 y 則進(jìn)入原來編輯的基礎(chǔ)上進(jìn)行修改。下面的例子是輸少了一個(gè) * 號而提示格式錯(cuò)誤需要重新修改。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃作業(yè)
no crontab for root - using an empty one
*/2 * * * date >> 2minute.txt ## 內(nèi)容輸少一個(gè) * 號
~
~
~
:wq ## 保存并退出
"/tmp/crontab.eYnQoX":1: bad day-of-week
errors in crontab file, can't install.
Do you want to retry the same edit? ## 格式錯(cuò)誤,系統(tǒng)會提示出錯(cuò),并問是否進(jìn)行修改
Do you want to retry the same edit? n ## 當(dāng)輸入 n 的時(shí)候,退出 crontab 編輯
crontab: edits left in /tmp/crontab.eYnQoX
[root@localhost ~]#
[root@localhost ~]# crontab -l ## 查詢當(dāng)前的 crontab 也是沒有任務(wù)
no crontab for root
[root@localhost ~]#
Do you want to retry the same edit? y ## 輸入 y,則進(jìn)入原來的編輯基礎(chǔ)進(jìn)行修改
*/2 * * * * date >> 2minute.txt ## 加上 1 個(gè) * 號
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## crontab 創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# ll ## 等待一段時(shí)間,已經(jīng)有 2minute.txt 這個(gè)文件,大小 87 字節(jié),最后修改時(shí)間 09:02
total 4
-rw-r--r--. 1 root root 87 Sep 26 09:02 2minute.txt
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# cat 2minute.txt ## 查看文件內(nèi)容
Sun Sep 26 08:58:01 CST 2021
Sun Sep 26 09:00:01 CST 2021
Sun Sep 26 09:02:01 CST 2021
[root@localhost ~]#
同理,調(diào)用腳本的方式也是這樣,只要 crontab 的格式錯(cuò)誤也會提示重新編輯,輸入 n 退出 crontab,輸入 y 進(jìn)入編輯。下面的例子是 / 斜杠沒有 * 號 和 輸少了兩個(gè) * 號而提示格式錯(cuò)誤需要重新修改。
新建的腳本文件一定要有 x 可執(zhí)行權(quán)限。 chmod u+x 增加用戶的可執(zhí)行權(quán)限后再 crontab -e 編輯計(jì)劃作業(yè),編輯 crontab 的時(shí)候要寫清楚腳本的路徑,如 /root/mydate.sh 。如沒有寫明路徑會因路徑不明而導(dǎo)致執(zhí)行計(jì)劃任務(wù)沒有結(jié)果輸出。
[root@localhost ~]# vim mydate.sh ## 當(dāng)前目錄下創(chuàng)建 mydate.sh 腳本
date >> 2minute.txt ## 內(nèi)容:輸出 date 到 2minute.txt 文件
~
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# chmod u+x mydate.sh ## 賦予 x 執(zhí)行權(quán)限
[root@localhost ~]# ll
total 4
-rwxr--r--. 1 root root 20 Sep 26 09:09 mydate.sh ## 已有 x 權(quán)限的腳本
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# pwd ## 查看當(dāng)前路徑
/root
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃作業(yè)
no crontab for root - using an empty one
/2 * * /root/mydate.sh ## 輸入錯(cuò)誤的格式,/2 前沒有 * 號,而且后面也缺少兩個(gè) * 號
~
~
~
~
:wq ## 保存并退出
"/tmp/crontab.OAqHeq":1: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? ## 提示錯(cuò)誤,是否進(jìn)行修改
Do you want to retry the same edit? y ## 輸入 y 進(jìn)行修改
*/2 * * * * /root/mydate.sh ## 輸入正確的格式
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## crontab創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# ll ## 等待一段時(shí)間查看目錄
total 8
-rw-r--r--. 1 root root 116 Sep 26 09:42 2minute.txt ## 已有 2minute.txt 文件,大小 116 字節(jié),最后修改時(shí)間 09:42
-rwxr--r--. 1 root root 20 Sep 26 09:09 mydate.sh
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]# cat 2minute.txt ## 查看 cat 2minute.txt 內(nèi)容
Sun Sep 26 09:36:02 CST 2021
Sun Sep 26 09:38:01 CST 2021
Sun Sep 26 09:40:01 CST 2021
Sun Sep 26 09:42:01 CST 2021
[root@localhost ~]#
6)、指定時(shí)間輸出 date 。
例:每小時(shí)的 0分鐘(整點(diǎn)),15分鐘、30分鐘、45分鐘 輸出 date 時(shí)間到當(dāng)前目錄 date.txt 文件,如果沒有 date.txt 文件則會自動(dòng)創(chuàng)建一個(gè)。指定時(shí)間的輸出,不要分鐘區(qū)間的 * 號,直接寫上時(shí)間。單個(gè)時(shí)間參數(shù)直接取代 * 號的位置,多個(gè)時(shí)間參數(shù)用 ,逗號隔開。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃作業(yè)
no crontab for root - using an empty one
0,15,30,45 * * * * date >> date.txt ## 多個(gè)時(shí)間參數(shù)寫到分鐘的區(qū)間,用逗號隔開。不用寫 * 號
~
~
~
:wq ## 保存并退出
[root@localhost ~]# ll ## 等待一段時(shí)間,已經(jīng)有 date.txt 文件,大小 290 字節(jié),最后修改時(shí)間 12:30 分
total 4
-rw-r--r--. 1 root root 290 Sep 26 12:30 date.txt
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# cat date.txt ## 列出 date.txt 文件內(nèi)容
Sun Sep 26 10:15:01 CST 2021
Sun Sep 26 10:30:01 CST 2021
Sun Sep 26 10:45:01 CST 2021
Sun Sep 26 11:00:01 CST 2021
Sun Sep 26 11:15:01 CST 2021
Sun Sep 26 11:30:01 CST 2021
Sun Sep 26 11:45:01 CST 2021
Sun Sep 26 12:00:01 CST 2021
Sun Sep 26 12:15:01 CST 2021
Sun Sep 26 12:30:01 CST 2021
[root@localhost ~]#
- 用腳本形式指定時(shí)間輸出 date 。
例:每小時(shí)的 0分鐘(整點(diǎn)),10分鐘、20分鐘、30分鐘、40分鐘、50分鐘 輸出 date 時(shí)間到 當(dāng)前目錄 date.txt 文件,如果沒有 date.txt 文件則會自動(dòng)創(chuàng)建一個(gè)。
[root@localhost ~]# vim output_date.sh ## 當(dāng)前目錄下編輯 output_date.sh 腳本
date >> date.txt ## 內(nèi)容:輸入出 date 到 date.txt 文件
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# chmod u+x output_date.sh ## 賦予 output_date.sh 腳本 x 可執(zhí)行權(quán)限
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rwxr--r--. 1 root root 17 Sep 26 12:41 output_date.sh ## output_date.sh 已有 x 可執(zhí)行權(quán)限
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃作業(yè)
0,10,20,30,40,50 * * * * /root/output_date.sh ## 內(nèi)容:每小時(shí)的0分,10分,20分,30分,40分,50分 調(diào)用 output_date.sh 腳本
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# ll ## 等待一段時(shí)間,已有 date.txt 文件,大小 145 字節(jié),最后修改時(shí)間 13:30
total 8
-rw-r--r--. 1 root root 145 Sep 26 13:30 date.txt
-rwxr--r--. 1 root root 17 Sep 26 12:41 output_date.sh
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat date.txt ## 查看 date.txt 文件內(nèi)容
Sun Sep 26 12:50:01 CST 2021
Sun Sep 26 13:00:01 CST 2021
Sun Sep 26 13:10:01 CST 2021
Sun Sep 26 13:20:01 CST 2021
Sun Sep 26 13:30:01 CST 2021
[root@localhost ~]#
7)、例:每天晚上 22:23、22:24、22:25 輸出 date 到 mydate.txt 文件。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃作業(yè)
23 22 * * * date >> mydate.txt ## 22:23分輸出 date 到 mydate.txt 文件
24 22 * * * date >> mydate.txt ## 22:24分輸出 date 到 mydate.txt 文件
25 22 * * * date >> mydate.txt ## 22:25分輸出 date 到 mydate.txt 文件
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## crontab 創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# ll ## 等待 22:25分后查看當(dāng)前目錄,就會有 mydate.txt 文件生成
total 4
-rw-r--r--. 1 root root 87 Sep 26 22:25 mydate.txt ## 生成的 mydate.txt 文件
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容,就是 crontab 輸出的 date
Sun Sep 26 22:23:01 CST 2021
Sun Sep 26 22:24:01 CST 2021
Sun Sep 26 22:25:01 CST 2021
[root@localhost ~]#
- 用腳本形式完成:每天晚上 22:40、22:42、22:44 輸出 date 到 mydate.txt 文件。
[root@localhost ~]# vim date.sh ## 編輯 date.sh 腳本文件
date >> mydate.txt ## 腳本只記錄執(zhí)行內(nèi)容,date 輸出到 mydate.txt 文件
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# chmod u+x date.sh ## 賦予腳本文件的擁有者 x 執(zhí)行權(quán)限
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rwxr--r--. 1 root root 93 Sep 26 22:35 date.sh ## 擁有者有 x 執(zhí)行權(quán)限
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃
40 22 * * * /root/date.sh ## 22:40 分調(diào)用 /root/date.sh 腳本
42 22 * * * /root/date.sh ## 22:42 分調(diào)用 /root/date.sh 腳本
44 22 * * * /root/date.sh ## 22:44 分調(diào)用 /root/date.sh 腳本
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## crontab 計(jì)劃創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# ll ## 等待執(zhí)行時(shí)間過去后查看目錄內(nèi)容,已有 mydate.txt 文件
total 8
-rwxr--r--. 1 root root 19 Sep 26 22:38 date.sh ## 腳本
-rw-r--r--. 1 root root 87 Sep 26 22:44 mydate.txt ## 腳本輸出的文件
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容
Sun Sep 26 22:40:01 CST 2021
Sun Sep 26 22:42:01 CST 2021
Sun Sep 26 22:44:01 CST 2021
[root@localhost ~]#
8)、例:每天 1 點(diǎn)到 3 點(diǎn)的第 10 分鐘 和第 50 分鐘把 date 輸出到 1-3date.txt 文件。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃
## 1點(diǎn) 到 3 點(diǎn)的 10 分、50 分執(zhí)行 date 輸出到 1-3date.txt 文件
10,50 1-3 * * * date >> 1-3date.txt
~
~
~
:wq ## 保存并推出
crontab: installing new crontab
[root@localhost ~]#
- 例:每天 4 點(diǎn)到 6 點(diǎn)的第 20 分鐘 和第 40 分鐘用腳本形式把 date 輸出到 4-6date.txt 文件。
[root@localhost ~]# vim 4-6date.sh ## 編輯腳本并保存為 4-6date.sh
date >> 4-6date.txt ## 腳本內(nèi)容:date 輸出到 4-6date.txt 文件
~
~
~
:wq ## 保存并退出
[root@localhost ~]#
[root@localhost ~]# chmod u+x 4-6date.sh ## 賦予腳本擁有者 x 執(zhí)行權(quán)限
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rwxr--r--. 1 root root 20 Sep 27 00:55 4-6date.sh ## 擁有者 x 權(quán)限
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# crontab -e ## 編輯 crontab 計(jì)劃
## 一個(gè)用戶可擁有多個(gè)計(jì)劃作業(yè)
10,50 1-3 * * * date >> 1-3date.txt
20,40 4-6 * * * /root/4-6date.sh ## 每天 4 點(diǎn)到 6 點(diǎn)的第 20 分鐘 和第 40 分鐘調(diào)用腳本
~
~
~
:wq ## 保存并退出
[root@localhost ~]# ll ## 等待執(zhí)行時(shí)間過去后查看目錄內(nèi)容,已有 1-3date.txt、 4-6date.txt 文件
total 12
-rw-r--r--. 1 root root 174 Sep 27 03:50 1-3date.txt ## 1點(diǎn)-3點(diǎn) date 輸出的文件
-rwxr--r--. 1 root root 20 Sep 27 00:55 4-6date.sh ## 腳本
-rw-r--r--. 1 root root 174 Sep 27 06:40 4-6date.txt ## 4點(diǎn)-6點(diǎn) date 輸出的文件
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# cat 1-3date.txt ## 查看 1-3date.txt 內(nèi)容
Mon Sep 27 01:10:01 CST 2021
Mon Sep 27 01:50:01 CST 2021
Mon Sep 27 02:10:01 CST 2021
Mon Sep 27 02:50:01 CST 2021
Mon Sep 27 03:10:01 CST 2021
Mon Sep 27 03:50:01 CST 2021
[root@localhost ~]# cat 4-6date.txt ## 查看 4-6date.txt 內(nèi)容
Mon Sep 27 04:20:01 CST 2021
Mon Sep 27 04:40:01 CST 2021
Mon Sep 27 05:20:01 CST 2021
Mon Sep 27 05:40:01 CST 2021
Mon Sep 27 06:20:01 CST 2021
Mon Sep 27 06:40:01 CST 2021
[root@localhost ~]#
9)、例:每逢周 六、日的 1:00 輸出 date 到 mydate.txt。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃
0 1 * * 6,7 date >> mydate.txt ## 每逢周六、日的 1:00 輸出 date 到 mydate.txt
~
~
~
:wq ## 保存并退出
crontab: installing new crontab ## crontab 創(chuàng)建成功
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-25 0:59:00" ## 更改時(shí)間為周六 0:59
Sat Sep 25 00:59:00 CST 2021 ## 更改的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date ## 等待一會,查看時(shí)間
Sat Sep 25 01:00:22 CST 2021 ## 已過 crontab 設(shè)定的 1點(diǎn)整執(zhí)行時(shí)間
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 29 Sep 25 01:00 mydate.txt ## 已產(chǎn)生 mydate.txt 文件
drwxr-xr-x. 4 root root 36 Sep 11 08:53 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容,符合 crontab 設(shè)置的條件,周六晚 1 點(diǎn)整輸出
Sat Sep 25 01:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-26 0:59:00" ## 更改時(shí)間為周日 0:59
Sun Sep 26 00:59:00 CST 2021 ## 更改后的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date
Sun Sep 26 01:01:09 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 周日同樣符合要求的輸出結(jié)果
Sat Sep 25 01:00:01 CST 2021
Sun Sep 26 01:00:02 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-27 0:59:00" ## 更改時(shí)間為周一 0:59
Mon Sep 27 00:59:00 CST 2021 ## 更改后的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date
Mon Sep 27 01:00:24 CST 2021 ## 已過預(yù)設(shè)的執(zhí)行時(shí)間
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 由于周一不符合 crontab 預(yù)設(shè)的要求,所以沒有輸出 date
Sat Sep 25 01:00:01 CST 2021
Sun Sep 26 01:00:02 CST 2021
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-28 0:59:00" ## 更改時(shí)間為周二 0:59
Tue Sep 28 00:59:00 CST 2021 ## 更改后的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date ## 已過預(yù)設(shè)的執(zhí)行時(shí)間
Tue Sep 28 01:00:31 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 由于周二不符合 crontab 預(yù)設(shè)的要求,所以沒有輸出 date
Sat Sep 25 01:00:01 CST 2021
Sun Sep 26 01:00:02 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-10-2 0:59:00" ## 更改時(shí)間為10月2號 周六 0:59
Sat Oct 2 00:59:00 CST 2021 ## 更改后的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date ## 已過預(yù)設(shè)的執(zhí)行時(shí)間
Sat Oct 2 01:00:39 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 周六符合 crontab 預(yù)設(shè)要求,所以有 date 輸出
Sat Sep 25 01:00:01 CST 2021
Sun Sep 26 01:00:02 CST 2021
Sat Oct 2 01:00:02 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-10-3 0:59:00" ## 更改時(shí)間為10月3號 周日 0:59
Sun Oct 3 00:59:00 CST 2021 ## 更改后的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date ## 已過預(yù)設(shè)的執(zhí)行時(shí)間
Sun Oct 3 01:00:14 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 周日符合 crontab 預(yù)設(shè)要求,所以有 date 輸出
Sat Sep 25 01:00:01 CST 2021
Sun Sep 26 01:00:02 CST 2021
Sat Oct 2 01:00:02 CST 2021
Sun Oct 3 01:00:01 CST 2021
[root@localhost ~]#
10)、例:每周日的 00:00 輸出 date 到 mydate.txt。
[root@localhost ~]# crontab -e ## 創(chuàng)建 crontab 計(jì)劃
0 0 * * 7 date >> mydate.txt ## 周日的 00:00 輸出 date 到 mydate.txt
~
~
~
:wq
crontab: installing new crontab
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-4 23:59:00" ## 更改時(shí)間,9月4日周六 23:59分
Sat Sep 4 23:59:00 CST 2021 ## 已更改的時(shí)間
[root@localhost ~]#
[root@localhost ~]# date
Sun Sep 5 00:00:24 CST 2021 ## 0 點(diǎn)后是周日,執(zhí)行 crontab 的時(shí)間
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 29 Sep 5 00:00 mydate.txt ## 已生成 mydate.txt 文件
drwxr-xr-x. 4 root root 36 Sep 11 2021 test
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容,已有 5 號周日輸出的 date 記錄
Sun Sep 5 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-11 23:59:00" ## 再把時(shí)間更改到 9 月 11 號 周六 23:59
Sat Sep 11 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Sun Sep 12 00:00:08 CST 2021 ## 0 點(diǎn)后是周日,執(zhí)行 crontab 的時(shí)間
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容,已新增 12 號周日輸出的 date 記錄
Sun Sep 5 00:00:01 CST 2021
Sun Sep 12 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-18 23:59:00" ## 再把時(shí)間更改到 9 月 18 號 周六 23:59
Sat Sep 18 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date ## 0 點(diǎn)后是周日,執(zhí)行 crontab 的時(shí)間
Sun Sep 19 00:00:58 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容,已新增 19 號周日輸出的 date 記錄
Sun Sep 5 00:00:01 CST 2021
Sun Sep 12 00:00:01 CST 2021
Sun Sep 19 00:00:01 CST 2021
[root@localhost ~]#
## -------- 已下是更改一些非周日的時(shí)間。
## -------- 通過測試 ,非周日即使過了 0 點(diǎn),也不會輸出 date 到 mydate.txt 文件
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-22 23:59:00" ## 把時(shí)間更改到 9 月 22 號 周三 23:59
Wed Sep 22 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Thu Sep 23 00:00:54 CST 2021 ## 9月23日 0點(diǎn),周四不符合 crontab 預(yù)設(shè)要求,所以不會輸出 date
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt
Sun Sep 5 00:00:01 CST 2021
Sun Sep 12 00:00:01 CST 2021
Sun Sep 19 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-27 23:59:00" 把時(shí)間更改到 9 月 27 號 周一 23:59
Mon Sep 27 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Tue Sep 28 00:00:48 CST 2021 ## 9月28日 0點(diǎn),周二不符合 crontab 預(yù)設(shè)要求,所以不會輸出 date
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt
Sun Sep 5 00:00:01 CST 2021
Sun Sep 12 00:00:01 CST 2021
Sun Sep 19 00:00:01 CST 2021
[root@localhost ~]#
11)、例:每月的 1號、10號、20號、30號的 3 點(diǎn) 輸出 date 到 mydate.txt。
[root@localhost ~]# crontab -e ## 編輯 crontab 計(jì)劃
0 3 1,10,20,30 * * date >> mydate.txt ## 每月的 1號、10號、20號、30號的 3 點(diǎn) 輸出 date 到 mydate.txt
~
~
~
:wq
crontab: installing new crontab
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-1 2:59:00" ## 把時(shí)間更改到 9 月 1 號 周三 2:59
Wed Sep 1 02:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Wed Sep 1 03:00:31 CST 2021
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 29 Sep 1 03:00 mydate.txt ## 3點(diǎn)整,生成 mydate.txt 文件
drwxr-xr-x. 4 root root 36 Sep 11 2021 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看文件內(nèi)容
Wed Sep 1 03:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-10 2:59:00" ## 把時(shí)間更改到 9 月 10 號 周五 2:59
Fri Sep 10 02:59:00 CST 2021
[root@localhost ~]# date
Fri Sep 10 03:03:39 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 3點(diǎn)整生成第二條數(shù)據(jù)
Wed Sep 1 03:00:01 CST 2021
Fri Sep 10 03:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-20 2:59:00" ## 把時(shí)間更改到 9 月 20 號 周一 2:59
Mon Sep 20 02:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Mon Sep 20 03:00:21 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 3點(diǎn)整生成第三條數(shù)據(jù)
Wed Sep 1 03:00:01 CST 2021
Fri Sep 10 03:00:01 CST 2021
Mon Sep 20 03:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-30 2:59:00" ## 把時(shí)間更改到 9 月 30 號 周四 2:59
Thu Sep 30 02:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Thu Sep 30 03:00:31 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 3點(diǎn)整生成第三條數(shù)據(jù)
Wed Sep 1 03:00:01 CST 2021
Fri Sep 10 03:00:01 CST 2021
Mon Sep 20 03:00:01 CST 2021
Thu Sep 30 03:00:01 CST 2021
[root@localhost ~]#
## -------- 已下是更改一些非1號、10號、20號、30號的時(shí)間。
## -------- 通過測試 ,非1號、10號、20號、30號即使過了 3 點(diǎn),也不會輸出 date 到 mydate.txt 文件
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-13 2:59:00" ## 時(shí)間設(shè)置為 9月13日 2:59:00
Mon Sep 13 02:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Mon Sep 13 03:00:08 CST 2021 ## 過了預(yù)設(shè)的時(shí)間 3 點(diǎn)
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 由于不是預(yù)設(shè)的 1號、10號、20號、30號,所以沒有 date 輸出
Wed Sep 1 03:00:01 CST 2021
Fri Sep 10 03:00:01 CST 2021
Mon Sep 20 03:00:01 CST 2021
Thu Sep 30 03:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-28 2:59:00" ## 時(shí)間設(shè)置為 9月28日 2:59:00
Tue Sep 28 02:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Tue Sep 28 03:00:55 CST 2021 ## 過了預(yù)設(shè)的時(shí)間 3 點(diǎn)
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 由于不是預(yù)設(shè)的 1號、10號、20號、30號,所以沒有 date 輸出
Wed Sep 1 03:00:01 CST 2021
Wed Sep 1 03:00:01 CST 2021
Fri Sep 10 03:00:01 CST 2021
Mon Sep 20 03:00:01 CST 2021
Thu Sep 30 03:00:01 CST 2021
[root@localhost ~]#
12)、例:3月、6月、9月、12月的第 30 日 00:00 輸出 date 到 mydate.txt 文件。
[root@localhost ~]# crontab -e ## 編輯 crontab 計(jì)劃
0 0 30 3,6,9,12 * date >> mydate.txt ## 3月、6月、9月、12月的第 30 日 00:00 輸出 date 到 mydate.txt 文件
~
~
~
:wq ## 保存并退出
crontab: installing new crontab
[root@localhost ~]#
[root@localhost ~]# date -s "2021-3-29 23:59:00" ## 把時(shí)間更改到 3 月 29 號 周一 23:59
Mon Mar 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date ## 3月30日 0 點(diǎn)以后
Tue Mar 30 00:00:18 CST 2021
[root@localhost ~]#
[root@localhost ~]# ll ## 0點(diǎn)過去后生成 mydate.txt 文件
total 4
-rw-r--r--. 1 root root 29 Mar 30 00:00 mydate.txt
drwxr-xr-x. 4 root root 36 Sep 11 2021 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 查看 mydate.txt 文件已有 3月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-6-29 23:59:00" ## 把時(shí)間更改到 6 月 29 號 周二 23:59
Tue Jun 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Wed Jun 30 00:00:30 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 0點(diǎn)過去后, 查看 mydate.txt 文件已新增 6月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
Wed Jun 30 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-29 23:59:00" ## 把時(shí)間更改到 9 月 29 號 周三 23:59
Wed Sep 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Thu Sep 30 00:00:48 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 0點(diǎn)過去后, 查看 mydate.txt 文件已新增 9月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
Wed Jun 30 00:00:01 CST 2021
Thu Sep 30 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-12-29 23:59:00" ## 把時(shí)間更改到 12 月 29 號 周三 23:59
Wed Dec 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Thu Dec 30 00:01:01 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 0點(diǎn)過去后, 查看 mydate.txt 文件已新增 12月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
Wed Jun 30 00:00:01 CST 2021
Thu Sep 30 00:00:01 CST 2021
Thu Dec 30 00:00:01 CST 2021
[root@localhost ~]#
## -------- 已下是更改一些非3月、6月、9月、12月的29號時(shí)間。
## -------- 通過測試 ,非3月、6月、9月、12月的29號時(shí)間即使過了 0 點(diǎn)進(jìn)入30號,也不會輸出 date 到 mydate.txt 文件
[root@localhost ~]#
[root@localhost ~]# date -s "2021-5-29 23:59:00" ## 把時(shí)間更改到 5 月 29 號 周六 23:59
Sat May 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Sun May 30 00:02:40 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 0 點(diǎn)過后沒有輸出 5月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
Wed Jun 30 00:00:01 CST 2021
Thu Sep 30 00:00:01 CST 2021
Thu Dec 30 00:00:01 CST 2021
[root@localhost ~]#
[root@localhost ~]# date -s "2021-11-29 23:59:00" ## 把時(shí)間更改到 11 月 29 號 周一 23:59
Mon Nov 29 23:59:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Tue Nov 30 00:00:32 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 0 點(diǎn)過后沒有輸出 11月30 的數(shù)據(jù)
Tue Mar 30 00:00:01 CST 2021
Wed Jun 30 00:00:01 CST 2021
Thu Sep 30 00:00:01 CST 2021
Thu Dec 30 00:00:01 CST 2021
[root@localhost ~]#
13)、每隔 1 天 2:30 輸出 date 到 mydate.txt 文件。
[root@localhost ~]# crontab -e ## 編輯 crontab 計(jì)劃
30 2 */2 * * date >> mydate.txt ## 每隔 1 天 2:30 輸出 date 到 mydate.txt 文件
~
~
~
:wq ## 保存并退出
[root@localhost ~]# date -s "2:29:00" ## 把時(shí)間更改為 2:29
Mon Sep 27 02:29:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Mon Sep 27 02:30:46 CST 2021
[root@localhost ~]#
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 29 Sep 27 02:30 mydate.txt ## 2:30 生成 mydate.txt
drwxr-xr-x. 4 root root 36 Sep 19 10:31 test
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt
Mon Sep 27 02:30:01 CST 2021 ## 27號 2:30 的數(shù)據(jù)
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-28 2:29:00" ## 把時(shí)間改為 9月28日 2:29
Tue Sep 28 02:29:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Tue Sep 28 02:30:37 CST 2021
[root@localhost ~]# cat mydate.txt ## 28號屬于第二天,不會輸出數(shù)據(jù)
Mon Sep 27 02:30:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-29 2:29:00" ## 把時(shí)間改為 9月29日 2:29
Wed Sep 29 02:29:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Wed Sep 29 02:30:52 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 29號屬于第三天,中間隔開了 28號,所以輸出 29號的數(shù)據(jù)
Mon Sep 27 02:30:01 CST 2021
Wed Sep 29 02:30:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-9-30 2:29:00" ## 把時(shí)間改為 9月30日 2:29
Thu Sep 30 02:29:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Thu Sep 30 02:30:58 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 29號輸出了數(shù)據(jù),30號不會輸出數(shù)據(jù)
Mon Sep 27 02:30:01 CST 2021
Wed Sep 29 02:30:01 CST 2021
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# date -s "2021-10-1 2:29:00" ## 把時(shí)間改為 10月1日 2:29
Fri Oct 1 02:29:00 CST 2021
[root@localhost ~]#
[root@localhost ~]# date
Fri Oct 1 02:30:25 CST 2021
[root@localhost ~]#
[root@localhost ~]# cat mydate.txt ## 30號沒有生成數(shù)據(jù),1號可以輸出數(shù)據(jù)
Mon Sep 27 02:30:01 CST 2021
Wed Sep 29 02:30:01 CST 2021
Fri Oct 1 02:30:01 CST 2021
[root@localhost ~]#
- 以上是記錄了 crontab 命令進(jìn)行的計(jì)劃任務(wù)測試,有直接通過 crontab -e 進(jìn)行編輯。也有先編輯腳本后通過 crontab -e 調(diào)用腳本的兩種方式。






