開發啓動rsync服務的腳本

rsync服務的重要性不言而喻,可是通常咱們都是rsync --daemon啓動,bash

咱們能夠啓動rsync服務在init.d目錄下呢?只要咱們寫個腳本就OK了。ide

[root@zyj ~]# cat /etc/init.d/rsyncd
#!/bin/bash
#created by sanpang
#email:zyjqianfuyu@163.com
#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/ 目錄下的服務號重疊rest


[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

到此測試結果以下:code

[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 [肯定]
相關文章
相關標籤/搜索