開發啓動rsync服務的腳本

rsync服務的重要性不言而喻,但是一般我們都是rsync --daemon啓動,

我們可以啓動rsync服務在init.d目錄下呢?只要我們寫個腳本就OK了。

[root@zyj ~]# cat /etc/init.d/rsyncd
#!/bin/bash
#created by sanpang
#email:[email protected]
#home:lovers.blog.51cto.com
#qq:791880666
#function   This script is used to monitor if the file is a malicious changes
# Source function library.
. /etc/rc.d/init.d/functions
start(){
   if [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 1 ]; then
      echo "the rsync is started"
      action   "rsync start"      /bin/false
      exit 0
   fi
   rsync --daemon
   sleep 2
   if [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 1 ]; then
      action   "rsync start"      /bin/true
      exit 0
   fi
}
stop(){
   if [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 0 ]; then
      echo "the rsync is stopped"
      action   "rsync stop"       /bin/false
      exit 0
   fi
   pkill rsync
   sleep 2
   if  [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 0 ]; then
      action   "rsync stop"       /bin/false
   fi
}
restart(){
   if [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 0 ]; then
      rsync --daemon
      action    "rsync stop"       /bin/true
      exit 0
   fi
   if [ "`ps -ef | grep  "rsync --daemon"  | grep -v "grep" | wc -l`" -eq 1 ]; then
         rsync --daemon
      action   "rsync stop"       /bin/true
      action   "rsync start"       /bin/true
   fi
}
case $1 in
start|START)
    start
    RETVAL=$?
;;
stop|STOP)
    stop
    RETVAL=$?
;;
restart|RESTART)
    restart
    RETVAL=$?
;;
*)
     echo "you must input start|stop|restart"
;;
esac

當然我們也可以設置rsync爲開機自啓動服務(添加如下代碼)

#function   This script is used to monitor if the file is a malicious changes
# chkconfig: - 45 80
# description: rsync is used to monitor if the file is a malicious changes
# probe: true
# config: /etc/init.d/rsyncd
# Source function library.
. /etc/rc.d/init.d/functions

其中45是服務開啓的號,80是服務停止的號,注意不要和/etc/rc.d/rc3.d/ 目錄下的服務號重疊


[root@zyj ~]# ls /etc/rc.d/rc3.d/
K01dnsmasq         K10cups        K69rpcsvcgssd  K85messagebus   K88wpa_supplicant  K99cpuspeed
K01smartd          K10psacct      K72autofs      K85rpcgssd      K89dund            K99lvm2-monitor
K02avahi-daemon    K10tcsd        K73ypbind      K85rpcidmapd    K89hidd            K99microcode_ctl

到此測試結果如下:

[root@zyj ~]# /etc/init.d/rsyncd start
the rsync is started
rsync start [失敗]
[root@zyj ~]# /etc/init.d/rsyncd stop
已終止
[root@zyj ~]# /etc/init.d/rsyncd start
rsync start [確定]
[root@zyj ~]# /etc/init.d/rsyncd restart
rsync stop [確定]
rsync start [確定]


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章