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


 

由於版本的迭代,最近剛剛接觸 CentOS 7,各種蛋疼 發現跟以前用的CentOS 6有着一些本質上的差別,連啟動服務都不會啟動了,一怒之下自己找資料,於是有了這篇文章...

 

 

 

1.建立服務文件

文件路徑

vim /usr/lib/systemd/system/nginx.service

 

服務文件內容

[Unit]Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target

[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]WantedBy=multi-user.target

 

文件內容解釋

[Unit]:服務的說明

Description:描述服務

After:描述服務類別

 

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

Type=forking是后台運行的形式

ExecStart為服務的具體運行命令

ExecReload為重啟命令

ExecStop為停止命令

PrivateTmp=True表示給服務分配獨立的臨時空間

注意:啟動、重啟、停止命令全部要求使用絕對路徑

 

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

 

2.保存目錄

754的權限保存在目錄:

/usr/lib/systemd/system 

 

3.設置開機自啟動

任意目錄下執行

systemctl enable nginx.service 

 

4.其他命令

啟動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

 

 

5.CentOS7.0中systemctl啟動關閉服務的用法

systemctl是主要的工具,它融合之前service和chkconfig的功能於一體。可以使用它永久性或只在當前會話中啟用/禁用服務。
systemctl可以列出正在運行的服務狀態
systemd-cgls以樹形列出正在運行的進程,它可以遞歸顯示控制組內容。

啟動/關閉、啟用/禁用服務:
啟動一個服務:systemctl start postfix.service
關閉一個服務:systemctl stop postfix.service
重啟一個服務:systemctl restart postfix.service
顯示一個服務的狀態:systemctl status postfix.service

在開機時啟用一個服務:systemctl enable postfix.service
在開機時禁用一個服務:systemctl disable postfix.service
查看服務是否開機啟動:systemctl is-enabled postfix.service;echo $?

查看已啟動的服務列表:systemctl list-unit-files|grep enabled

說明:啟用服務就是在當前“runlevel”的配置文件目錄/etc/systemd/system/multi-user.target.wants/里,建立/usr/lib/systemd/system里面對應服務配置文件的軟鏈接;禁用服務就是刪除此軟鏈接。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM