
[if !vml]

[endif]

[if !vml]

[endif]

Sgcal
3w1h??????? 框架
Linux系統(tǒng)定時任務Cron(d)服務應用實踐
系統(tǒng)定時任務:
[if !supportLists]1、 [endif]什么是定時任務?
周期性得執(zhí)行任務計劃得軟件,Linux定時任務得常用軟件crond。
[if !supportLists]2、 [endif]使用定時任務軟件,可以每天,每小時按你得需求重復得執(zhí)行一項工作。
例如:備份??? 都是0點以后,2點爬起來備份,4點以后睡覺。
需要寫一個程序實現(xiàn)自動備份,然后讓定時軟件幫助你執(zhí)行。
鬧鐘。。。。。??梢宰非笈笥选?。。
[if !supportLists]3、 [endif]怎樣用。
[if !supportLists](1)????[endif]用戶定時任務計劃
[if !vml]

[endif]
要想配置定時任務,首先啟動cron服務。
[if !vml]

[endif]
開啟自啟動:
[if !vml]

[endif]
[root@quyunlong~]# systemctl status crond
● crond.service - Command Scheduler
??Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendorpreset: enabled)
??Active: active (running) since Thu 2019-03-21 16:39:43 CST; 17h ago
?MainPID: 6684 (crond)
??CGroup: /system.slice/crond.service
??????????└─6684 /usr/sbin/crond -n
Mar 21 16:39:43 quyunlong systemd[1]:
Started Command Scheduler.
Mar 21 16:39:43 quyunlong crond[6684]:
(CRON) INFO (RANDOM_DELAY will be...)
Mar 21 16:39:44 quyunlong crond[6684]:
(CRON) INFO (running with inotify...)
Hint: Some lines were ellipsized, use -l to
show in full.
[root@quyunlong~]#
Crontab 命令是用來設置定時任務規(guī)則得配置命令
定時任務內容存放得位置/var/spool/cron/
以當前用戶明作為文件名
-l????? 查看已經(jīng)設置得定時任務
-e???? edit編輯定時任務
-u???? 查看特定用戶下得定時任務
Crontab –l?==cat? /var/spool/cron/root
Crontab –e?==vim? /var/spool/cron/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
# |?|? |? |? |
# *?*? *? *? *command to be executed
共六列:
第一列:分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
第六列:要執(zhí)行得任務命令程序
特殊符號:
*表示得每或每一天得意思
00 23 * * * cmd
-連續(xù)分隔符 1-10
00 8-23 * * * cmd
,列舉??? 1,2,3,4,8,
[if !supportLists]00???[endif]1,2,3,4,8,* * * cmd
/n? n是數(shù)字。
*/10 * * * * cmd

[if !vml]
[endif]
[if !supportLists](2)????[endif]系統(tǒng)定時任務計劃
[if !supportLists]1.?????[endif]不用管理員干預,系統(tǒng)自動執(zhí)行。
[if !supportLists]2.?????[endif]也可以利用系統(tǒng)任務為管理員服務。

[if !vml]
[endif]
=============================================
*/1?*?*?*?*?/bin/sh?/scripts/[if !vml]
[endif]data.sh
30?3,12?*?*?*?/bin/sh?/scripts/[if !vml]
[endif]oldboy.sh
30?*/6?*?*?*????/bin/sh?/scripts/[if !vml]
[endif]oldboy.sh
30?8-18/2?*?*?*?/bin/sh?/scripts/[if !vml]
[endif]oldboy.sh
30?21?*?*?*
最強大腦
45?4?1,10,22?*?*?/application/apache/bin/apachectl?graceful
10?1?*?*?6,0?/application/apache/bin/apachectl?graceful
0,30?18-23?*?*?*?/application/apache/bin/apachectl?graceful
00?*/1?*?*?*?/application/apache/bin/apachectl?graceful
#################
*?23,00-07/1?*?*?*?/application/apache/bin/apachectl?graceful
00?*/1?*?*?*
00?11?*?4?1-3?/application/apache/bin/apachectl?graceful
每周日上午9:30去老男孩教育上課每天上午8:30去老男孩教育上課,這是脫產(chǎn)班的上課頻率。
30 9 * * */0 cmd
30 8?* * * cmd
實踐:
1
、每分鐘追加一次oldboy字符串到/tmp/oldboy.log里
[if !vml]

[endif]


[if !vml]

[endif]

[if !supportLists]1、 [endif]命令行執(zhí)行
[if !supportLists]2、 [endif]盡量用腳本文件實現(xiàn)
[if !vml]

[endif]

范例13-3:每天晚上0點,把站點目錄/var/www/html下的內容打包備份到/data目錄下,并且要求每次生成不同的備份包名。
至少分三步:
1.
命令行稿成功準備工作:
[root@oldboyedu?~]#?mkdir?/var/www/html?/data?-p
具體命令:
[root@oldboyedu?~]#?tar?zcvf?/data/html_$(date?+%F).tar.gz?/var/www/html/
tar:?Removing?leading?`/'?from?member?names
/var/www/html/
[root@oldboyedu?~]#?ll?/data
total?4
-rw-r--r--?1?root?root?117?Mar?22?11:59?html_2019-03-22.tar.gz
2
、編寫定時任務
[root@oldboyedu?~]#?crontab?-l
00?00?*?*?*?tar?zcvf?/data/html_$(date?+%F).tar.gz?/var/www/html/
錯了,不執(zhí)行。。。??磮箦e日志。。。錯誤日志定時任務沒有執(zhí)行,出錯了日志在哪里?出錯日志/var/log/cron
Mar?22?12:03:01?oldboyedu?CROND[18770]:?(root)?CMD?(tar?zcvf?/data/html_$(date?+)
定時任務對%的要求,百分號要轉義。\%
正確答案:
[root@oldboyedu?~]#?crontab?-l
00?00?*?*?*?tar?zcvf?/data/html_$(date?+\%F).tar.gz?/var/www/html/
優(yōu)秀的答案(企業(yè)里):
1.
命令行執(zhí)行
cd?/var/www
tar?zcf?/data/html_$(date?+%F).tar.gz?./html
2.
盡量用腳本文件實現(xiàn)
#mkdir?/server/scripts?-p?#
存放腳本的文件
cd?/server/scripts
#cat[if !vml]
[endif]bak.sh
cd?/var/www
tar?zcf?/data/html_$(date?+%F).tar.gz?./html
命令行執(zhí)行:
/bin/sh?/server/scripts/[if !vml]
[endif]bak.sh
3
、編輯定時任務
#bak?html?dir?by?oldboy?at?2020108
00?00?*?*?*?/bin/sh?/server/scripts/[if !vml]
[endif]bak.sh?&>/dev/null
[root@oldboyedu?/server/scripts]#?crontab?-l|tail?-2
#bak?html?dir?by?oldboy?at?2020108
00?00?*?*?*?/bin/sh?/server/scripts/[if !vml]
[endif]bak.sh?&>/dev/null
4
、檢查正式結果
[root@oldboyedu?/server/scripts]#?ls?-l?/data
運維規(guī)范:
腳本位置:/server/scripts
軟件位置:/server/tools
編譯位置:/application/軟件名字-版本,然后要軟連接。

[if !vml]


[endif]

[if !vml]

[endif]

[if !vml]

[endif]

物??人???時間???
考試:考到定時任務。
下周
用戶管理1天
磁盤管理:2-3天
網(wǎng)絡基礎:4天 2天基礎??2天實踐
Shell基礎:2天
三劍客深入1-2天
進程管理命令、安裝命令1天
第一階段結束:
第二階段,搭建集群。
mso-far