systemctl 命令是系統服務管理指令,
假設 Linux 上有安裝 postgresql,
啟動服務 start service
sudo systemctl start postgresqlor
sudo systemctl start postgresql.service結尾要不要加入 .service 都可以(真實檔案其實結尾是有 .service)
停止服務 stop service
sudo systemctl stop postgresql重啟服務 restart service
sudo systemctl restart postgresql查看當前服務狀態 show status of service
sudo systemctl status postgresql目前為沒有啟動
查看服務設定檔
sudo systemctl cat postgresql如下輸出, 第一行就是指你的設定檔路徑
# /lib/systemd/system/postgresql.service
# postgresql.service is the meta unit for managing all PostgreSQL clusters on
# the system at once. Conceptually, this unit is more like a systemd target,
# but we are using a service since targets cannot be reloaded.
#
# The unit actually managing PostgreSQL clusters is [email protected],
# instantiated as [email protected] for individual clusters.
[Unit]
Description=PostgreSQL RDBMS
[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/bin/true
RemainAfterExit=on
[Install]
WantedBy=multi-user.target
查看更詳細的服務設定檔
sudo systemctl show postgresql啟動開機自動啟動 enable service ( auto-start )
sudo systemctl enable postgresql停止開機自動啟動 disable service ( not auto-start )
sudo systemctl disable postgresql查看 postgresql 是否 active
sudo systemctl is-active postgresql查看 postgresql 是否有設定自動啟動
sudo systemctl is-enabled postgresql查看 postgresql 是否啟動失敗
sudo systemctl is-failed postgresql查看全部已啟動的服務
sudo systemctl list-units查看全部已啟動的 service 服務
sudo systemctl list-units --type=service查看全部服務狀態
sudo systemctl list-unit-files --type service -all編輯服務設定,
通常我是直接進入 /etc/systemd/system 修改,
但是也可以透過以下的方式修改
sudo systemctl edit --full postgresql預設是 nano EDITOR, 可改成自己喜歡的 EDITOR,
這邊用 vim 做示範, 輸入以下指令
export SYSTEMD_EDITOR=vim再執行以下指令
sudo visudo之後會跳出編輯介面, 在裡面加上一行,
Defaults env_keep += "SYSTEMD_EDITOR"
這樣預設就是用 vim 打開了 😄
重新載入 systemd
sudo systemctl daemon-reload實際應用可參考 在 Linux 中自動啟動 docker
