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