歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> CentOS 7.x自定義開機啟動設置

CentOS 7.x自定義開機啟動設置

日期:2017/3/3 17:46:10   编辑:學習Linux

CentOS 7.x自定義開機啟動設置

CentOS 7.x自定義開機啟動設置


一:概述

CentOS 7繼承了RHEL 7的新的特性,例如強大的systemctl,而systemctl的使用也使得以往系統服務的/etc/init.d/的啟動腳本的方式就此改變,也大幅提高了系統服務的運行效率。但服務的配置和以往也發生了極大的不同。

CentOS 7的服務systemctl腳本存放在:/usr/lib/systemd/,有系統(system)和用戶(user)之分,像需要開機不登陸就能運行的程序,還是存在系統服務裡吧,即:/usr/lib/systemd/system目錄下,並且每一個服務以 .service 結尾。

Centos 系統服務腳本目錄:

/usr/lib/systemd

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

/usr/lib/systemd/system

2. 開始配置

2.1 環境===>

1. 添加自啟動服務為:uwsgi

2. 自啟動腳本以經寫好

3. 本人將開機自啟動的腳本放在 /etc/init.d/目錄下,並加執行權限

2.2 配置 ===>

每一個服務以.service結尾,一般會分為3部分:[Unit]、[Service]和[Install],我寫的這個服務用於開機運行uwsgi項目,具體內容如下:

[root@tsingserver~]#cd/usr/lib/systemd/system[root@tsingserversystem]#vimuwsgid.service[Unit]Description=uwsgidAfter=network.target[Service]Type=forkingExecStart=/etc/init.d/uwsgidstartExecReload=/etc/init.d/uwsgidrestartExecStop=/etc/init.d/uwsgidstopPrivateTmp=true[Install]WantedBy=multi-user.target

服務腳本按照上面編寫完成後,以 754 的權限保存在/usr/lib/systemd/system目錄下

[root@tsingserversystem]#chmod754uwsgid.service

配置文件解釋如下:

[Unit] ===> 服務的說明

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

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

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

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

--------------------------------------------------------------------------------

3. 設置開機自啟動

systemctlenableuwsgid.service

4. 其他命令

以下以httpd服務為例

任務舊指令新指令使某服務自動啟動chkconfig --level 3 httpd on systemctl enable httpd.service使某服務不自動啟動chkconfig --level 3 httpd offsystemctl disable httpd.service檢查服務狀態service httpd status

systemctl statushttpd.service

<== 服務詳細信息

systemctl is-active httpd.service

<==僅顯示是否 Active

顯示所有已啟動的服務chkconfig --listsystemctl list-units --type=service啟動某服務service httpd startsystemctl start httpd.service停止某服務service httpd stopsystemctl stop httpd.service重啟某服務service httpd restartsystemctl restart httpd.service

啟動nginx服務

systemctlstartnginx.service

設置開機自啟動

systemctlenablenginx.service

停止開機自啟動

systemctldisablenginx.service

查看服務當前狀態

systemctlstatusnginx.service

重新啟動服務

systemctlrestartnginx.service

查看所有已啟動的服務

systemctllist-units--type=service

參考博文:http://blog.csdn.net/yuanguozhengjust/article/details/38019923

http://www.centoscn.com/CentOS/config/2015/0507/5374.html

http://xxxxxx/Linuxjc/1184768.html TechArticle

Copyright © Linux教程網 All Rights Reserved