linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron

linux任務計劃:在某個時間自動執行命令或者腳本。

任務計劃的配置文件
cat /etc/crontab

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

前面兩行是定義變量,第三行是指發送郵件給誰,然後最後一行有五個*(星號)分別對應着五個位,也就是上面的五行,下面來介紹一下分別表示什麼意思:

1.表示分鐘(0-59)

2.表示小時(0-23)

3.表示日期(1-31)

4.表示月份(1-12可以寫數字或者英文的簡寫)

5.表示星期(0-6,0或者7表示週日,也可以寫成英文的簡寫)

最後一行開頭部分是用戶(在root用戶下不寫默認就是root)

後面部分,也就是com開頭的位置是你要執行的命令。

自定義任務計劃(用法和vim一樣)
crontab -e

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

案例:每天3點執行123.sh腳本(星號就是所有,比如第三個是 意思就是1-31日都執行)
0 3 * * * /bin/bash /usr/local/sbin/123.sh

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

把正確的結果和錯誤的結果都輸出到123.log裏
0 3 * * * /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log

把正確的結果和錯誤的結果都追加到123.log裏
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

每隔兩個月的1-10號的星期二和星期五的三點 可以寫成如下格式。0是分鐘,3是小時,1-10是日期1號到10號,星號/2是月份除以2可以整除的那個月份,2,5是這個月的週二和週五
0 3 1-10 */2 2,5

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

設置好之後我們還需要使用systemctl start crond命令啓動cron才能啓用計劃
systemctl start crond

查看是否啓用計劃
ps aux |grep crom

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

或者使用systemctl start crom(crom是你的計劃名,圖片中紅色的就是計劃名)
systemctl start crom

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹綠色就代表已經啓用了)

有時候計劃了執行腳本,但是沒有執行很可能是沒有使用絕對路徑,解決辦法要麼在計劃裏把該條命令的路徑添加到PATH裏,要麼就使用絕對路徑。

還有就是建議計劃的任務都要寫上正確和錯誤的結果追加到某個文件裏,這樣才能保證任務有據可查。

-l 列出,查看任務計劃列表
crontab -l

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

任務計劃cron的文件在/var/spool/cron/目錄下,如果是root的cron就是/var/spool/cron/root(cat查看)

-r刪除計劃
crontab -r

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

-u指定用戶
crontab -u root -l(指定查看root的任務計劃)

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

chkconfig服務管理工具

chkconfig服務管理工具(主要在centos6及以前的系統中使用,如怎麼控制服務啓動,如何控制服務開機啓動、如何控制服務在指定級別啓動等等)

查看當前系統中使用chkconfig的服務(只會列出sysv服務管理模式的進程,centos7很多的都是systemd模式的)
chkconfig --list

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

使用如上命令在centos7只能查看到兩個服務,如下圖

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

這兩個進程文件在如下路徑(之後我們可以腳本放到這個目錄裏用chkconfig工具管理也是可以的)
ls /etc/init.d/

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

表示該服務在0-6級別分別是什麼狀態,開機啓動還是關閉(network是需要關閉或開啓的服務名,off是關閉on是開啓)
chkconfig network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

0-6這七個級別,每個級別的含義

0級別是關機狀態、1級別是單用戶、2是多用戶模式(沒有nfs服務)、3是是用戶模式(不帶圖形)、4保留的級別、5多用戶模擬(帶圖形)、6重啓

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

指定network的3級別關閉
chkconfig --level 3 network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

指定network的3級別和5級別關閉(3跟5中間不加任何符號,也不加空格)
chkconfig --level 35 network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

把123加入到服務列表裏來(需要把123放到/etc/init.d目錄下來,並且確定123是個腳本,同是需要有如下兩行格式才能識別)
chkconfig --add 123

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

從服務列表刪除
chkconfig --del 123

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

systemd管理服務

systemd是centos7的管理服務的機制。

列出systemd模式的服務
systemctl list-units --all --type=service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

讓服務開機啓動,service是服務名(顯示出來的是軟連接,然後ls這個軟連接就可以得到服務的文件地址)
systemctl enable crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

不讓服務開機啓動,service是服務名
systemctl disable crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看服務狀態,service是服務名
systemctl status crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

停止服務,service是服務名
systemctl stop crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

啓動服務,service是服務名
systemctl start crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

重啓服務,service是服務名
systemctl restart crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

檢查服務是否開機啓動,service是服務名(enabled就是啓動)
systemctl is-enabled crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

unit介紹

上一節課裏systemctl enable crond獲得的服務文件所在目錄,也就是ls /usr/lib/systemd/system,目錄裏的所有文件都叫做unit。

unit主要分爲一下幾種類型。

service 系統服務

target 多個unit組成的組

device 硬件設備

mount 文件系統掛載點

automount 自動掛載點

path 文件或路徑

scope 不是由systemd啓動的外部進程

slice 進程組

snapshot systemd快照

socket 進程間通信套接字

swap swap文件
linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

timer 定時器

unit相關的命令
列出正在運行的unit
systemctl list-units

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出所有,包括失敗的或者inactive的
systemctl list-units --all

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出inactive的unit
systemctl list-units --all --state=inactive

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出狀態爲active的service
systemctl list-units --type=service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看某個服務是否爲active
systemctl is-active crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

target介紹

系統爲了方便管理用target來管理unit

查看所有target。
systemctl list-unit-files --type=target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看指定target下面有哪些unit。
systemctl list-dependencies multi-user.target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看系統默認的target。
systemctl get-default

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

設置默認的target。
systemctl set-default multi-user.target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

一個service屬於一種類型的unit
多個unit組成了一個target
一個target裏面包含了多個service

查看指定sevice屬於那個target。(看install位置)
cat /usr/lib/systemd/system/sshd.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron

linux任務計劃:在某個時間自動執行命令或者腳本。

任務計劃的配置文件
cat /etc/crontab

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

前面兩行是定義變量,第三行是指發送郵件給誰,然後最後一行有五個*(星號)分別對應着五個位,也就是上面的五行,下面來介紹一下分別表示什麼意思:

1.表示分鐘(0-59)

2.表示小時(0-23)

3.表示日期(1-31)

4.表示月份(1-12可以寫數字或者英文的簡寫)

5.表示星期(0-6,0或者7表示週日,也可以寫成英文的簡寫)

最後一行開頭部分是用戶(在root用戶下不寫默認就是root)

後面部分,也就是com開頭的位置是你要執行的命令。

自定義任務計劃(用法和vim一樣)
crontab -e

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

案例:每天3點執行123.sh腳本(星號就是所有,比如第三個是 意思就是1-31日都執行)
0 3 * * * /bin/bash /usr/local/sbin/123.sh

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

把正確的結果和錯誤的結果都輸出到123.log裏
0 3 * * * /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log

把正確的結果和錯誤的結果都追加到123.log裏
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

每隔兩個月的1-10號的星期二和星期五的三點 可以寫成如下格式。0是分鐘,3是小時,1-10是日期1號到10號,星號/2是月份除以2可以整除的那個月份,2,5是這個月的週二和週五
0 3 1-10 */2 2,5

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

設置好之後我們還需要使用systemctl start crond命令啓動cron才能啓用計劃
systemctl start crond

查看是否啓用計劃
ps aux |grep crom

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

或者使用systemctl start crom(crom是你的計劃名,圖片中紅色的就是計劃名)
systemctl start crom

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹綠色就代表已經啓用了)

有時候計劃了執行腳本,但是沒有執行很可能是沒有使用絕對路徑,解決辦法要麼在計劃裏把該條命令的路徑添加到PATH裏,要麼就使用絕對路徑。

還有就是建議計劃的任務都要寫上正確和錯誤的結果追加到某個文件裏,這樣才能保證任務有據可查。

-l 列出,查看任務計劃列表
crontab -l

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

任務計劃cron的文件在/var/spool/cron/目錄下,如果是root的cron就是/var/spool/cron/root(cat查看)

-r刪除計劃
crontab -r

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

-u指定用戶
crontab -u root -l(指定查看root的任務計劃)

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

chkconfig服務管理工具

chkconfig服務管理工具(主要在centos6及以前的系統中使用,如怎麼控制服務啓動,如何控制服務開機啓動、如何控制服務在指定級別啓動等等)

查看當前系統中使用chkconfig的服務(只會列出sysv服務管理模式的進程,centos7很多的都是systemd模式的)
chkconfig --list

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

使用如上命令在centos7只能查看到兩個服務,如下圖

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

這兩個進程文件在如下路徑(之後我們可以腳本放到這個目錄裏用chkconfig工具管理也是可以的)
ls /etc/init.d/

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

表示該服務在0-6級別分別是什麼狀態,開機啓動還是關閉(network是需要關閉或開啓的服務名,off是關閉on是開啓)
chkconfig network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

0-6這七個級別,每個級別的含義

0級別是關機狀態、1級別是單用戶、2是多用戶模式(沒有nfs服務)、3是是用戶模式(不帶圖形)、4保留的級別、5多用戶模擬(帶圖形)、6重啓

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

指定network的3級別關閉
chkconfig --level 3 network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

指定network的3級別和5級別關閉(3跟5中間不加任何符號,也不加空格)
chkconfig --level 35 network off

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

把123加入到服務列表裏來(需要把123放到/etc/init.d目錄下來,並且確定123是個腳本,同是需要有如下兩行格式才能識別)
chkconfig --add 123

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

從服務列表刪除
chkconfig --del 123

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

systemd管理服務

systemd是centos7的管理服務的機制。

列出systemd模式的服務
systemctl list-units --all --type=service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

讓服務開機啓動,service是服務名(顯示出來的是軟連接,然後ls這個軟連接就可以得到服務的文件地址)
systemctl enable crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

不讓服務開機啓動,service是服務名
systemctl disable crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看服務狀態,service是服務名
systemctl status crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

停止服務,service是服務名
systemctl stop crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

啓動服務,service是服務名
systemctl start crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

重啓服務,service是服務名
systemctl restart crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

檢查服務是否開機啓動,service是服務名(enabled就是啓動)
systemctl is-enabled crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

unit介紹

上一節課裏systemctl enable crond獲得的服務文件所在目錄,也就是ls /usr/lib/systemd/system,目錄裏的所有文件都叫做unit。

unit主要分爲一下幾種類型。

service 系統服務

target 多個unit組成的組

device 硬件設備

mount 文件系統掛載點

automount 自動掛載點

path 文件或路徑

scope 不是由systemd啓動的外部進程

slice 進程組

snapshot systemd快照

socket 進程間通信套接字

swap swap文件
linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

timer 定時器

unit相關的命令
列出正在運行的unit
systemctl list-units

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出所有,包括失敗的或者inactive的
systemctl list-units --all

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出inactive的unit
systemctl list-units --all --state=inactive

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

列出狀態爲active的service
systemctl list-units --type=service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看某個服務是否爲active
systemctl is-active crond.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

target介紹

系統爲了方便管理用target來管理unit

查看所有target。
systemctl list-unit-files --type=target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看指定target下面有哪些unit。
systemctl list-dependencies multi-user.target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

查看系統默認的target。
systemctl get-default

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

設置默認的target。
systemctl set-default multi-user.target

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

一個service屬於一種類型的unit
多個unit組成了一個target
一個target裏面包含了多個service

查看指定sevice屬於那個target。(看install位置)
cat /usr/lib/systemd/system/sshd.service

linux任務計劃cron、chkconfig工具、systemd管理、unit介紹、targe介紹

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章