rsync服务的重要性不言而喻,但是一般我们都是rsync --daemon启动,

我们可以启动rsync服务在init.d目录下呢?只要我们写个脚本就OK了。

[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/ 目录下的服务号重叠


[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 [确定]