基於CentOS7系統添加自定義腳本服務及參數說明【轉】


概述

centos6如果要添加自定義腳本服務只需要把腳本放到/etc/init.d然后授權后用chkconfig添加后就可以管理了,那么centos7又是怎么添加自定義腳本服務呢?


CentOS7添加自定義腳本服務說明

在CentOS7下,已經不再使用chkconfig命令管理系統開機自啟動服務和條件自定義腳本服務了,而是使用管理unit的方式來控制開機自啟動服務和添加自定義腳本服務。在/usr/lib/systemd/system目錄下包含了各種unit文件,有service后綴的服務unit,有target后綴的開機級別unit等。

如果想把自定義的腳本變成服務進程,都需要寫對應的service配置文件,這樣才能被unit所管理(注意:自定義開機自啟動服務的.service配置文件必須放在/usr/lib/systemd/system這個目錄下面)。服務類別又分為服務又分為系統服務(system)和用戶服務(user)。

  • 系統服務:開機不登陸就能運行的程序(常用於開機自啟)。
  • 用戶服務:需要登陸以后才能運行的程序。

編寫.service配置文件說明

1、[unit]區塊:設置管理啟動順序與依賴關系

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

注意:如果After、Before、Wants、Requires等號后面需要填寫多個服務可以用空格隔開。After和Before字段只涉及啟動順序,不涉及依賴關系。Wants字段與Requires字段只涉及依賴關系,與啟動順序無關,默認情況下是同時啟動的。

2、[Service]區塊:設置啟動行為

•啟動命令

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

所有的啟動設置之前,都可以加上一個連詞號(-),表示"抑制錯誤",即發生錯誤的時候,不影響其他命令的執行。例如:ExecStop=-/bin/sh /server/scripts/xx.sh

•啟動類型 Type字段定義啟動類型。它可以設置的值如下:

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

•重啟行為

KillMode字段,定義Systemd如何停止服務,它可以設置的值如下

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

Restart字段,定義了服務退出后,Systemd的重啟方式,它可以設置的值如下

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

•service區塊的其他一些字段

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

3、[Install]區塊:定義如何安裝這個配置文件,即怎樣做到開機啟動

這個設置非常重要,如果設置開機自啟動,在/etc/systemd/system目錄下面的multi-user.target.wants子目錄之中機會創建一個服務的軟鏈接

WantedBy字段,表示該服務所在的 Targe,target的含義是服務組,表示一組服務,它可以設置的值如下

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

配置文件目錄

基於CentOS7系統添加自定義腳本服務及參數說明,附實例

實例--配置一個自定義腳本服務

這里寫一個rsync+inotify的腳本服務

1、創建腳本目錄

mkidr -p /server/scripts/sync.sh
vim /server/scripts/sync.sh

2、腳本代碼

#!/bin/bash
#chkconfig: 2345 38 46
. /etc/init.d/functions
if [ $# -ne 1 ]
then
 echo "usage: $0 {start|stop|status}"
 exit 1
fi
case "$1" in
start)
 if [ -e "/var/run/inotify.pid" ]
 then
 action "inotify service start fail" /bin/false
 echo "sync server is running......"
 sleep 1
 exit 1
 fi
 /bin/bash /server/scripts/inotify.sh &
 `ps -ef|grep "inotifywait"|grep -v "grep"|awk '{print $2}'` >/var/run/inotify.pid
 if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
 then
 action "inotify service is started" /bin/true
 else
 action "inotify service is started" /bin/false
 fi
 ;;
stop)
 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -a -e "/var/run/inotify.pid" ]
 then
 rm -f /var/run/inotify.pid >/dev/null 2>&1
 pkill inotifywait
 else
 action "inotify service stop fail" /bin/false
 echo "sync server is not running"
 sleep 1
 exit 1
 fi
 sleep 1
 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 -a ! -e "/var/run/inotify.pid" ]
 then
 action "inotify service is stoped" /bin/true
 else
 action "inotify service is stoped" /bin/false
 fi
 ;;
status)
 if [ `ps -ef|grep inotify|wc -l` -gt 2 ]
 then
 action "inotify service is running"
 else
 action "inotify service is stoped"
 fi
 ;;
*)
 echo "usage: $0 {start|stop|status}"
 exit 1
esac

 

3、添加注冊腳本服務文件(vim /usr/lib/systemd/system/syncd.service),文件內容如下

[Unit]
Description="rsync+inotify實時同步服務"
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/bin/sh /server/scripts/sync.sh start
ExecReload=/bin/sh /server/scripts/sync.sh restart
ExecStop=/bin/sh /server/scripts/sync.sh stop
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target

 

4、運行systemctl start syncd命令啟動服務

 

轉自

基於CentOS7系統添加自定義腳本服務及參數說明,附實例 https://www.toutiao.com/i6723191120836215304/


免責聲明!

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



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