systemd常用命令及配置自定義服務開機自啓動

ubuntu16之後,系統改成了用systemd來管理服務,systemd的主要命令是systemctl。

一. 常用命令的使用

sshd服務爲例:

查找自啓服務

$ sudo systemctl list-unit-files | grep enabled

開機自啓

$ sudo systemctl enable sshd

禁止開機自啓

$ sudo systemctl disable sshd

停止服務

$ systemctl stop sshd

啓動服務

$ systemctl start sshd

檢查某個服務的狀態

$ systemctl status sshd

可以用上面的方法找出和關閉一些不用的服務,如藍牙bluetooth,網絡打印服務cups.service,cups-browsed.service.

二. 配置自啓動服務

配置文件主要放在/usr/lib/systemd/system目錄,也可能在/etc/systemd/system目錄,在該目錄下新建一個文件(如hello.serivce)並修改其內容。

[Unit]
Description=this is hello service

[Service]
Type=simple
ExecStart= <shell command>
ExecStop=<shell command>

[Install]
WantedBy=multi-user.target

主要是在ExecStart和ExecStop中寫啓動和停止服務的shell命令,ExecStop可缺省。
配置完成後,reload一下systemd

$ sudo systemctl daemon-reload

然後即可通過systemctl的命令來啓動和停止服務,及實現開機自啓。

具體內容及做法可參考文章:
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章