CentOS 7.x設定自訂開機啟動,添加自訂系統服務

來源:互聯網
上載者:User

標籤:

Centos 系統服務指令碼目錄:

  1. /usr/lib/systemd/  

有系統(system)和使用者(user)之分,

如需要開機沒有登陸情況下就能啟動並執行程式,存在系統服務(system)裡,即:

  1. /lib/systemd/system/  

反之,使用者登入後才能啟動並執行程式,存在使用者(user)裡

服務以.service結尾。

這邊以nginx開機運行為例

1.建立服務檔案
  1. vim /lib/systemd/system/nginx.service  
  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的許可權儲存在目錄:

  1. /lib/systemd/system  
3.設定開機自啟動

 

  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

CentOS 7.x設定自訂開機啟動,添加自訂系統服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.