systemctl

systemctl是一個systemd工具,Systemd是一個系統管理守護進程、工具和庫的集合
systemd作爲父守護進程運行(PID = 1)
systemd與init進程的區別
它與init進程的主要差別如下。
(1)默認的 RunLevel(在/etc/inittab文件設置)現在被默認的 Target 取代,位置是/etc/systemd/system/default.target,通常符號鏈接到graphical.target(圖形界面)或者multi-user.target(多用戶命令行)。
(2)啓動腳本的位置,以前是/etc/init.d目錄,符號鏈接到不同的 RunLevel 目錄 (比如/etc/rc3.d、/etc/rc5.d等),現在則存放在/lib/systemd/system和/etc/systemd/system目錄。
(3)配置文件的位置,以前init進程的配置文件是/etc/inittab,各種服務的配置文件存放在/etc/sysconfig目錄。現在的配置文件主要存放在/lib/systemd目錄,在/etc/systemd目錄裏面的修改可以覆蓋原始設置。


基礎命令

查看systemd版本:systemd --version
查看systemd的二進制文件和庫的安裝位置:whereis systemd
查看\systemctl的二進制文件和庫的安裝位置:whereis systemctl
分析systemd啓動過程: systemd-analyze
分析每個進程在引導時花費的時間:systemd-analyze blame
分析啓動時的關鍵鏈: systemd-analyze critical-chain
按層次列出控制組:systemd-cgls
根據CPU,內存,輸入和輸出列出控制組:systemd-cgtop
列出所有運行單元:systemctl list-units
列出所有失敗的單元:systemctl --failed


list-unit-files

在/usr/lib/systemd/system/目錄下包含四種類型文件.mount,.service,.target,.wants
列出所有可用的單元:systemctl list-unit-files
列出所有mount:systemctl list-unit-files --type=mount
列出所有socket:systemctl list-unit-files --type=socket
列出所有service:systemctl list-unit-files --type=service


service

systemctl會根據/usr/lib/systemd/system/下的service啓動相應的進程

列出所有service:systemctl list-unit-files --type=service
分析service的關鍵鏈:systemd-analyze critical-chain httpd.service
獲取service的依賴項列表:systemctl list-dependencies httpd.service
service啓動,重啓,停止…

# systemctl start httpd.service   //啓動
# systemctl restart httpd.service //重啓
# systemctl stop httpd.service    //停止
# systemctl reload httpd.service  //重新加載
# systemctl status httpd.service  //狀態
# systemctl enable httpd.service  //啓用
# systemctl disable httpd.service //禁止
# systemctl is-enabled httpd.service //是否啓用
# systemctl mask httpd.service     //屏蔽服務
# systemctl unmask httpd.service     //取消屏蔽服務
# systemctl show httpd             //檢查service詳細信息

獲取service當前cpu份額:systemctl show -p CPUShares httpd.service
將service的cpu份額限制爲2000:systemctl set-property httpd.service CPUShares=2000
注意:爲服務設置CPUShare時,會創建一個名爲service的目錄(httpd.service.d),其中包含一個包含CPUShare Limit信息的文件90-CPUShares.conf


創建一個新的service

  1. add /lib/systemd/system/af_watchdog.service
    watchdog_daemon是service要啓動的程序,af_recovery.sh是watchdog_daemon執行的腳本
    [Unit]
    Description=Little Kernel Watchdog

    [Service]
    Type=simple
    ExecStart=/usr/sbin/watchdog_daemon -f /usr/local/share/af_watchdog/af_recovery.sh
    Restart=on-failure
    RestartSec=3s

    [Install]
    WantedBy=multi-user.target
  1. execute command to enable af_watchdog by systemctl
    systemctl enable af_watchdog.service
    systemctl start af_watchdog.service
    systemctl staus af_watchdog.service

參考文章

  1. systemctl
發佈了271 篇原創文章 · 獲贊 146 · 訪問量 125萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章