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

一、 linux任務計劃cron
關於cron任務計劃功能的操作都是通過crontab這個命令來完成的。
其中常用的選項有:
-u :指定某個用戶,不加-u選項則爲當前用戶;
/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開頭的位置是你要執行的命令。
crontab -e定義任務計劃(用法和vim一樣)
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
從左到右:在12月5日(這一天必須是星期2)的10點01分執行命令 echo “ok” > /root/cron.log

編寫格式:分 時 日 月 周 user command
-l :列出計劃任務;
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
-r :刪除計劃任務
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
注:
crontab -e 實際上是打開了 “/var/spool/cron/username” (如果是root則打開的是/var/spool/cron/root)這個文件,所以不要直接去編輯那個文件,因爲可能會出錯,所以一定要使用 crontab -e 來編輯,另外備份的話,直接複製一份這個目錄下的文件即可

制定計劃建議都使用追加命令 >>,把正確和錯誤的都追加進一個文件裏記錄。

二、chkconfig工具(系統服務管理)
其實這就是系統所有的預設服務了,如network,cron 等等服務(service 服務名 start|stop|restart)

1、chkconfig –list 查看使用chkconfig這個工具的服務端有哪些
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
注:0:關機狀態
1:單用戶
2:無NFS支持的多用戶模式
3:完全多用戶模式
4:保留給用戶自定義
5:圖形登錄方式
6:重啓
ls /etc/init.d/ //服務端的啓動腳本都放在這個目錄下,只有啓動腳本放在這個目錄下才能加入系統服務中。
把network的3級別打開/關閉:
chkconfig --level 3 network off/on
(不加 –level 3 就是將0-6個級別都關掉)
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe

刪除network服務端:
chkconfig –del network

添加network服務端(在/etc/init.d/目錄下添加了啓動腳本後需要用這個命令才能把服務加入到系統服務中):
chkconfig –add network
三、 systemd管理服務

查看所有的系統服務:
systemctl list-unit-files
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
列出所有的service的服務情況(如果不加all,不激活狀態的就不會列出來):
systemctl list-units –all –type=service
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
幾個常用的服務相關的命令
讓某個服務開機啓動(.service可以省略):
systemctl enable crond.service

不讓開機啓動:
systemctl disable crond

查看狀態:
systemctl status crond

停止服務:
systemctl stop crond

啓動服務:
systemctl start crond

重啓服務:
systemctl restart crond

檢查服務是否開機啓動:
systemctl is-enabled crond

unit介紹

系統所有unit,分爲以下類型:
ls /usr/lib/systemd/system
service:系統服務
target:多個unit組成的組
device:硬件設備
mount:文件系統掛載點
automount:自動掛載點
path:文件或路徑
scope:不是由systemd啓動的外部進程
slice:進程組
snapshot:systemd快照
socket:進程間通信套接字
swap:swap文件
timer:定時器
以上每種類型的文件都爲一個unit,正式這些unit才組成了系統的各個資源(各個服務,各個設備等)。
unit相關的命令

列出正在運行(active)的unit:
systemctl list-units

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

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

列出所有狀態的service:
systemctl list-units –all –type=service

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

查看某個服務是否爲active:
systemctl is-active crond.service
四、unit相關的命令:

系統所有unit,分爲以下類型:
ls /usr/lib/systemd/system
service:系統服務
target:多個unit組成的組
device:硬件設備
mount:文件系統掛載點
automount:自動掛載點
path:文件或路徑
scope:不是由systemd啓動的外部進程
slice:進程組
snapshot:systemd快照
socket:進程間通信套接字
swap:swap文件
timer:定時器
以上每種類型的文件都爲一個unit,正式這些unit才組成了系統的各個資源(各個服務,各個設備等)。

列出正在運行(active)的unit:
systemctl list-units

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

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

列出所有狀態的service:
systemctl list-units –all –type=service

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

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

五、target介紹

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

列出系統所有的target:
systemctl list-unit-files –type=target
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe
查看指定target下面有哪些unit,如下列的multi-user:
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

一個service屬於一種類型的unit

多個unit組成了一個target

一個target裏面包含了多個service

cat /usr/lib/systemd/system/sshd.service //看[install]部分,定義了該service屬於哪一個target
linux任務計劃cron、chkconfig工具、systemd管理服務、unit介紹和targe

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