systemctl
行程與系統
用法
systemctl <verb> <unit>啟動、停止和查看 systemd 服務;`enable` 只決定開機行為,想現在也啟動要用 `enable --now`,改過 unit 檔後必須 `daemon-reload`。
常用選項
| 選項 | 含義 |
|---|---|
| status <unit> | Whether it is running, its PID, and the last log lines. |
| start / stop / restart | Act on the service now, without changing boot behaviour. |
| reload | Re-read the config without dropping connections, if the service supports it. |
| enable --now <unit> | Start it now and also start it at every boot. |
| daemon-reload | Required after you edit a .service file, before restarting it. |
| list-units --failed | Everything that failed to start. |
| --user | Act on your own user units instead of the system ones. |
範例
sudo systemctl restart nginxRestart the web server.
systemctl status sshd --no-pagerPrint the state and recent log lines without opening a pager.
sudo systemctl enable --now dockerStart Docker and make it come back after a reboot.
看什麼在跑、什麼在吃機器。先弄清是什麼,再決定殺不殺。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. systemctl 是做什麼的?
啟動、停止和查看 systemd 服務;`enable` 只決定開機行為,想現在也啟動要用 `enable --now`,改過 unit 檔後必須 `daemon-reload`。
Q. 怎麼寫?
systemctl <verb> <unit> —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 7 個,完整列表在 man systemctl。這條命令屬於行程與系統。
相關命令
man 手冊: man systemctl