下怎么运行sh脚本_基于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]區塊:設置管理啟動順序與依賴關系
注意:如果After、Before、Wants、Requires等號后面需要填寫多個服務可以用空格隔開。After和Before字段只涉及啟動順序,不涉及依賴關系。Wants字段與Requires字段只涉及依賴關系,與啟動順序無關,默認情況下是同時啟動的。
2、[Service]區塊:設置啟動行為
?啟動命令
所有的啟動設置之前,都可以加上一個連詞號(-),表示"抑制錯誤",即發生錯誤的時候,不影響其他命令的執行。例如:ExecStop=-/bin/sh /server/scripts/xx.sh
?啟動類型 Type字段定義啟動類型。它可以設置的值如下:
?重啟行為
KillMode字段,定義Systemd如何停止服務,它可以設置的值如下
Restart字段,定義了服務退出后,Systemd的重啟方式,它可以設置的值如下
?service區塊的其他一些字段
3、[Install]區塊:定義如何安裝這個配置文件,即怎樣做到開機啟動
這個設置非常重要,如果設置開機自啟動,在/etc/systemd/system目錄下面的multi-user.target.wants子目錄之中機會創建一個服務的軟鏈接
WantedBy字段,表示該服務所在的 Targe,target的含義是服務組,表示一組服務,它可以設置的值如下
配置文件目錄
實例--配置一個自定義腳本服務
這里寫一個rsync+inotify的腳本服務
1、創建腳本目錄
mkidr -p /server/scripts/sync.shvim /server/scripts/sync.sh2、腳本代碼
#!/bin/bash#chkconfig: 2345 38 46. /etc/init.d/functionsif [ $# -ne 1 ]then echo "usage: $0 {start|stop|status}" exit 1ficase "$1" instart) 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 1esac3、添加注冊腳本服務文件(vim /usr/lib/systemd/system/syncd.service),文件內容如下
[Unit]Description="rsync+inotify實時同步服務"After=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/bin/sh /server/scripts/sync.sh startExecReload=/bin/sh /server/scripts/sync.sh restartExecStop=/bin/sh /server/scripts/sync.sh stopKillSignal=SIGQUITTimeoutStopSec=5KillMode=processPrivateTmp=true[Install]WantedBy=multi-user.target4、運行systemctl start syncd命令啟動服務
后面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注下~
總結
以上是生活随笔為你收集整理的下怎么运行sh脚本_基于CentOS7系统添加自定义脚本服务及参数说明,附实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有没人看了武媚娘传奇的,怎么样,好看吗?
- 下一篇: 推理计算过程_转导推理—Transduc