CentOS 7.X設置自定義開機啓動,添加自定義系統服務



Centos 系統服務腳本目錄:
[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. /usr/lib/systemd/  
有系統(system)和用戶(user)之分,

如需要開機沒有登陸情況下就能運行的程序,存在系統服務(system)裏,即:

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. /lib/systemd/system/  
反之,用戶登錄後才能運行的程序,存在用戶(user)裏

服務以.service結尾。

這邊以nginx開機運行爲例

1.建立服務文件

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. vim /lib/systemd/system/nginx.service  
[plain] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. [Unit]  
  2. Description=nginx  
  3. After=network.target  
  4.    
  5. [Service]  
  6. Type=forking  
  7. ExecStart=/www/lanmps/init.d/nginx start  
  8. ExecReload=/www/lanmps/init.d/nginx restart  
  9. ExecStop=/www/lanmps/init.d/nginx  stop  
  10. PrivateTmp=true  
  11.    
  12. [Install]  
  13. WantedBy=multi-user.target  

 

[Unit]:服務的說明

Description:描述服務
After:描述服務類別

[Service]服務運行參數的設置

Type=forking是後臺運行的形式
ExecStart爲服務的具體運行命令
ExecReload爲重啓命令
ExecStop爲停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啓動、重啓、停止命令全部要求使用絕對路徑

[Install]服務安裝的相關設置,可設置爲多用戶

2.保存目錄

以754的權限保存在目錄:

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. /lib/systemd/system  

3.設置開機自啓動

 

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
 
  1. systemctl enable nginx.service  

 

4.其他命令

 

 

任務 舊指令 新指令
使某服務自動啓動 chkconfig --level 3 httpd  on              systemctl enable httpd.service
使某服務不自動啓動 chkconfig --level 3 httpd off systemctl disable httpd.service
檢查服務狀態 service httpd status systemctl status httpd.service (服務詳細信息) 
systemctl is-active httpd.service (僅顯示是否 Active)
顯示所有已啓動的服務 chkconfig --list systemctl list-units --type=service
啓動某服務 service httpd start systemctl start httpd.service
停止某服務 service httpd stop systemctl stop httpd.service
重啓某服務 service httpd restart systemctl restart httpd.service

 

啓動nginx服務

systemctl start nginx.service

設置開機自啓動

systemctl enable nginx.service

停止開機自啓動

systemctl disable nginx.service

查看服務當前狀態

systemctl status nginx.service

重新啓動服務

systemctl restart nginx.service

查看所有已啓動的服務

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