如需要開機沒有登陸情況下就能運行的程序,存在系統服務(system)裏,即:
/lib/systemd/system/
反之,用戶登錄後才能運行的程序,存在用戶(user)裏
服務以.service結尾。
這邊以nginx開機運行爲例
1.建立服務文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/www/lanmps/init.d/nginx start
ExecReload=/www/lanmps/init.d/nginx restart
ExecStop=/www/lanmps/init.d/nginx stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、停止命令全部要求使用絕對路徑
[Install]服務安裝的相關設置,可設置爲多用戶
2.保存目錄
以754的權限保存在目錄:
/lib/systemd/system
3.設置開機自啓動
systemctl enable nginx.service
有可能會用到下面這一段:
添加httpd服務
# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
# vi /etc/rc.d/init.d/httpd --在文件開頭加上下面2句(注:第一行3個數字參數意義分別爲:哪些Linux級別需要啓動httpd(3,4,5);啓動序號(85);關閉序號(15))
# chkconfig: 345 85 15
# description: Activates/Deactivates Apache Web Server
添加httpd服務
# chkconfig -–add httpd