標籤:centos7自啟動服務
一:概述
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項目,具體內容如下:
[[email protected] ~]# cd /usr/lib/systemd/system[[email protected] system]# vim uwsgid.service[Unit]Description=uwsgidAfter=network.target[Service]Type=forkingExecStart=/etc/init.d/uwsgid startExecReload=/etc/init.d/uwsgid restartExecStop=/etc/init.d/uwsgid stopPrivateTmp=true [Install]WantedBy=multi-user.target
服務指令碼按照上面編寫完成後,以 754 的許可權儲存在/usr/lib/systemd/system目錄下
[[email protected] system]# chmod 754 uwsgid.service
設定檔解釋如下:
[Unit] ===> 服務的說明
Description:描述服務
After:描述服務類別
[Service] ===> 服務運行參數的設定
Type=forking:是後台啟動並執行形式
ExecStart:為服務的具體運行命令
ExecReload:為重啟命令
ExecStop:為停止命令
PrivateTmp=True:表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install] ===> 服務安裝的相關設定,可設定為多使用者
--------------------------------------------------------------------------------
3. 設定開機自啟動
systemctl enable uwsgid.service
4. 其他命令
以下以httpd服務為例
任務 |
舊指令 |
新指令 |
使某服務自動啟動 |
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
參考博文:http://blog.csdn.net/yuanguozhengjust/article/details/38019923
http://www.centoscn.com/CentOS/config/2015/0507/5374.html
本文出自 “馬鈴薯” 部落格,請務必保留此出處http://zhaoyulin.blog.51cto.com/11665101/1871435
CentOS 7.x自訂開機啟動設定