一、以nginx為例
使用yum命令安裝的nginx
Systemd服務文件以.service結尾,比如現在要建立nginx為開機啟動,如果用yum install命令安裝的,yum命令會自動創建nginx.service文件,直接用命令:
systemcel enable nginx.service //開機自啟
使用源碼編譯安裝的
1、手動創建nginx.service服務文件。并將其放入 /lib/systemd/system 文件夾中。
nginx.service內容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/data/nginx/sbin/nginx
ExecReload=/data/nginx/sbin/nginx -s reload
ExecStop=/data/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
PS: 注意上面的ExecStart/ExecReload/ExecStop 必須 以自己的為主
所對應的key說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后臺運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3
保存退出。
2、設置開機啟動
systemctl enable nginx.service
其他服務命令
systemctl start nginx.service (啟動nginx服務)
systemctl stop nginx.service (停止nginx服務)
systemctl enable nginx.service (設置開機自啟動)
systemctl disable nginx.service (停止開機自啟動)
systemctl status nginx.service (查看服務當前狀態)
systemctl restart nginx.service (重新啟動服務)
systemctl list-units --type=service (查看所有已啟動的服務)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。