[日常] Linux開機自啓服務----使用systemctl管理 (CentOS 7)

linux開機自啓服務的方法有很多,比如將啓動命令放到linux系統開機就會執行的那些文件中,或者寫個shell放到linux開機就會執行的文件夾裏 等等   

但上述方法在我自己實踐中都或多或少的存在問題,也不方便管理,因此想使用systemctl去管理自己的服務

查閱資料得到如下實踐結果:

先到這個文件夾下,這裏是放管理員服務的地方 (還有的是在 usr/lib/systemd/system/ 或 lib/systemd/system/ 中, 本文不討論)

/etc/systemd/system/

新建文件  命名

name.service              (name是你服務的名字,使用systemctl管理時就用這個名)

然後編輯這個文件   例如:

[Unit]
Description=chat-server
After=network.target

[Service]
Type=simple
ExecStart=/home/wgs/gotest/chat/chat-server &

[Install]
WantedBy=multi-user.target

 

詳細配置見:   ( https://www.freedesktop.org/software/systemd/man/systemd.service.html#Examples )

簡單來說:

Description=chat-server  表示你的說明

After=network.target  表示你的服務應該在啥服務啓動後再啓動

ExecStart=   你服務啓動的命令

更多的去看上面的連接和百度

 

編輯好後  使用 

systemctl daemon-reload

重新加載

然後使用

systemctl start yourname      啓動

systemctl status yourname    查看狀態

測試是否成功

之後使用

systemctl enable yourname

將它添加到開機自啓中

之後可以 shutdown -r now 重啓 看看成功沒

 

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