1. 程式人生 > >任務計劃cron / chkconfig工具/ system管理服務/unit及target介紹

任務計劃cron / chkconfig工具/ system管理服務/unit及target介紹

20180512

一、linux任務計劃cron

1、cat /etc/crontab 任務計劃的配制文件;
技術分享圖片

2、crontab -e 編輯配制文件。
技術分享圖片
圖片說明,每個月的1-10號3點執行腳本123.sh ,以追加形式生成日誌文件123.log,生成錯誤文件日誌321.log
crontab -e 實際上是打開了 “/var/spool/cron/username” (如果是root則打開的是/var/spool/cron/root)這個文件,所以不要直接去編輯那個文件,因為可能會出錯,所以一定要使用 crontab -e 來編輯,另外備份的話,直接復制一份這個目錄下的文件即可!
制定計劃建議都使用追加命令 >>,把正確和錯誤的都追加進一個文件裏記錄。

啟動服務 systemctl start crond ,才會生效。

二、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

任務計劃cron / chkconfig工具/ system管理服務/unit及target介紹