CentOS7 systemd添加自定義系統(tǒng)服務(wù)的方法
systemd:
CentOS 7的服務(wù)systemctl腳本存放在:/usr/lib/systemd/,有系統(tǒng)(system)和用戶(user)之分,即:/usr/lib/systemd/system ,/usr/lib/systemd/user
每一個(gè)服務(wù)以.service結(jié)尾,一般會(huì)分為3部分:[Unit]、[Service]和[Install],就以nginx為例吧,具體內(nèi)容如下:
創(chuàng)建service:
在/usr/lib/systemd/system下創(chuàng)建nginx.service文件內(nèi)容如下(看應(yīng)用需求也可以在 /usr/lib/systemd/usr下創(chuàng)建):
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit]
Description : 服務(wù)的簡單描述
Documentation : 服務(wù)文檔
After= : 依賴,僅當(dāng)依賴的服務(wù)啟動(dòng)之后再啟動(dòng)自定義的服務(wù)單元
[Service]
Type : 啟動(dòng)類型simple、forking、oneshot、notify、dbus
Type=simple(默認(rèn)值):systemd認(rèn)為該服務(wù)將立即啟動(dòng)。服務(wù)進(jìn)程不會(huì)fork。如果該服務(wù)要啟動(dòng)其他服務(wù),不要使用此類型啟動(dòng),除非該服務(wù)是socket激活型。 Type=forking:systemd認(rèn)為當(dāng)該服務(wù)進(jìn)程fork,且父進(jìn)程退出后服務(wù)啟動(dòng)成功。對(duì)于常規(guī)的守護(hù)進(jìn)程(daemon),除非你確定此啟動(dòng)方式無法滿足需求,使用此類型啟動(dòng)即可。使用此啟動(dòng)類型應(yīng)同時(shí)指定 PIDFile=,以便systemd能夠跟蹤服務(wù)的主進(jìn)程。 Type=oneshot:這一選項(xiàng)適用于只執(zhí)行一項(xiàng)任務(wù)、隨后立即退出的服務(wù)。可能需要同時(shí)設(shè)置 RemainAfterExit=yes 使得 systemd 在服務(wù)進(jìn)程退出之后仍然認(rèn)為服務(wù)處于激活狀態(tài)。 Type=notify:與 Type=simple 相同,但約定服務(wù)會(huì)在就緒后向 systemd 發(fā)送一個(gè)信號(hào)。這一通知的實(shí)現(xiàn)由 libsystemd-daemon.so 提供。 Type=dbus:若以此方式啟動(dòng),當(dāng)指定的 BusName 出現(xiàn)在DBus系統(tǒng)總線上時(shí),systemd認(rèn)為服務(wù)就緒。
PIDFile : pid文件路徑
ExecStartPre :啟動(dòng)前要做什么,上文中是測試配置文件 -t
ExecStart:啟動(dòng)
ExecReload:重載
ExecStop:停止
PrivateTmp:True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
[Install]
WantedBy:服務(wù)安裝的用戶模式,從字面上看,就是想要使用這個(gè)服務(wù)的有是誰?上文中使用的是:multi-user.target ,就是指想要使用這個(gè)服務(wù)的目錄是多用戶?!敢陨先莻€(gè)人理解,瞎猜的,如有不當(dāng),請(qǐng)大家多多指教」每一個(gè).target實(shí)際上是鏈接到我們單位文件的集合,當(dāng)我們執(zhí)行:
$ sudo systemctl enable nginx.service
就會(huì)在/etc/systemd/system/multi-user.target.wants/目錄下新建一個(gè)/usr/lib/systemd/system/nginx.service 文件的鏈接。
操作Service:
#啟動(dòng)服務(wù) $ sudo systemctl start nginx.service #查看日志 $ sudo journalctl -f -u nginx.service -- Logs begin at 四 2015-06-25 17:32:20 CST. -- 6月 25 10:28:24 Leco.lan systemd[1]: Starting nginx - high performance web server... 6月 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 6月 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful 6月 25 10:28:24 Leco.lan systemd[1]: Started nginx - high performance web server. #重啟 $ sudo systemctl restart nginx.service #重載 $ sudo systemctl reload nginx.service #停止 $ sudo systemctl stop nginx.service
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Linux操作系統(tǒng)下終端亂碼的完美解決方法
下面小編就為大家?guī)硪黄P(guān)于Linux操作系統(tǒng)下終端亂碼的完美解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
Ubuntu使用國內(nèi)源出現(xiàn)Hash Sum mismatch錯(cuò)誤的解決
這篇文章主要介紹了Ubuntu使用國內(nèi)源出現(xiàn)Hash Sum mismatch錯(cuò)誤的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
LAMP服務(wù)器性能優(yōu)化技巧之Linux主機(jī)優(yōu)化
LAMP服務(wù)器性能優(yōu)化技巧之Linux主機(jī)優(yōu)化:由于LAMP的發(fā)展迅速,如今LAMP已經(jīng)成為Web服務(wù)器的標(biāo)準(zhǔn)。如何才能優(yōu)化LAMP服務(wù)器的性能?2012-02-02

