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