Linux命令之--systemctl詳細理解及常用命令
Linux 服務管理兩種方式service和systemctl
systemd是Linux系統最新的初始化系統(init),作用是提高系統的啟動速度,儘可能啟動較少的程序,儘可能更多程序併發啟動。
systemd對應的程序管理命令是systemctl
1. systemctl命令相容了service
即systemctl也會去/etc/init.d目錄下,檢視,執行相關程式
systemctl redis start
systemctl redis stop
# 開機自啟動
systemctl enable redis
2. systemctl命令管理systemd的資源Unit
systemd的Unit放在目錄/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu)
systemctl –-version
1.檢視版本號
systemctl –-version
2.管理服務(unit)
systemctl 提供了一組子命令來管理單個的 unit,其命令格式為:
systemctl [command] [unit]
command 主要有:
start:立刻啟動後面接的 unit。
stop:立刻關閉後面接的 unit。
restart:立刻關閉後啟動後面接的 unit,亦即執行 stop 再 start 的意思。
reload:不關閉 unit 的情況下,重新載入配置檔案,讓設定生效。
enable:設定下次開機時,後面接的 unit 會被啟動。
disable:設定下次開機時,後面接的 unit 不會被啟動。
status:目前後面接的這個 unit 的狀態,會列出有沒有正在執行、開機時是否啟動等資訊。
is-active:目前有沒有正在執行中。
is-enable:開機時有沒有預設要啟用這個 unit。
kill :不要被 kill 這個名字嚇著了,它其實是向執行 unit 的程序傳送訊號。
show:列出 unit 的配置。
mask:登出 unit,登出後你就無法啟動這個 unit 了。
unmask:取消對 unit 的登出。
enable 和 disable 操作
比如我們為 etcd 服務建立了配置檔案 /lib/systemd/system/etcd.service,然後執行 enable 命令:
systemctl enable etcd.service
檢視 unit 的配置
使用 show 子命令可以檢視 unit 的詳細配置情況:
systemctl show etcd.service
登出與反登出 unit
如果我們想暫時的禁用某個 unit,比如 etcd.service,可以登出這個 unit,登出之後就無法再啟動這個服務了:
systemctl mask etcd.service
unmask 操作就是刪除掉 mask 操作中建立的連結。
檢視系統上的 unit
systemctl 提供了子命令可以檢視系統上的 unit,命令格式為:
systemctl [command] [--type=TYPE] [--all]
command 有:
list-units:列出當前已經啟動的 unit,如果新增 -all 選項會同時列出沒有啟動的 unit。
list-unit-files:根據 /lib/systemd/system/ 目錄內的檔案列出所有的 unit。
--type=TYPE:可以過濾某個型別的 unit。
不帶任何引數執行 systemctl 命令會列出所有已啟動的 unit:
列舉已經啟動的unit
systemctl list-units (或者直接 sudo systemctl)
————————————————
幾個簡單的指令用來切換操作模式,大致如下所示:
sudo systemctl poweroff # 系統關機
sudo systemctl reboot # 重新開機
sudo systemctl suspend # 進入暫停模式
sudo systemctl hibernate # 進入休眠模式
sudo systemctl rescue # 強制進入救援模式
sudo systemctl emergency # 強制進入緊急救援模式
————————————————
原文連結:https://blog.csdn.net/skh2015java/article/details/94012643