rsync服务一shell脚本管理

1.脚本内容

[root@rsync-A ~]# cat /scritp/2018-07-23/rsyncd 
#!/bin/bash
# chkconfig: 3 66 99
. /etc/rc.d/init.d/functions
Dir=/var/run/rsyncd.pid
if [ $# -ne 1 ];then
        echo "Error:$0 start|restart|stop"
        exit 1
fi
case $1 in
start)
        num=`netstat -lntup|grep 873|wc -l`
        if [ "$num" -ne 0 ];then
                 echo "rsync running ...."
        else
                /usr/bin/rsync --daemon 
                action "rsync start ..." /bin/true
        fi
;;
restart)
        num=`netstat -lntup|grep 873|wc -l`
        if [ "$num" -ne 0 ];then
                kill `cat $Dir` >/dev/null 2>&1
                sleep 1
                /usr/bin/rsync --daemon 
                action "rsync restart ..." /bin/true
        else
                /usr/bin/rsync --daemon
                action "rsync restart ..." /bin/true
        fi
;;
stop)
        num=`netstat -lntup|grep 873|wc -l`
        if [ "$num" -eq 0 ];then
                action "rsync stop ..." /bin/true
        else
                kill `cat $Dir` >/dev/null 2>&1
                sleep 1
                action "rsync stop ..." /bin/true
        fi
;;
*)
        echo "error:Input invalid"
;;
esac

2.设成开机自启动脚本

[root@rsync-A ~]# cp /scritp/2018-07-23/rsyncd /etc/init.d/
[root@rsync-A ~]# cd /etc/init.d/
[root@rsync-A init.d]# chmod +x rsyncd 
[root@rsync-A init.d]# chkconfig --add rsyncd 
[root@rsync-A init.d]# chkconfig --list rsyncd
rsyncd          0:off   1:off   2:off   3:on    4:off   5:off   6:off
[root@rsync-A init.d]# chkconfig rsyncd on
[root@rsync-A init.d]# chkconfig --list rsyncd
rsyncd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

3.验证

[root@rsync-A liang1]# /etc/init.d/rsyncd stop
rsync stop ...                                             [  OK  ]
[root@rsync-A liang1]# netstat -lntup|grep 873
[root@rsync-A liang1]# /etc/init.d/rsyncd start
rsync start ...                                            [  OK  ]
[root@rsync-A liang1]# netstat -lntup|grep 873 
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      3866/rsync          
tcp        0      0 :::873                      :::*                        LISTEN      3866/rsync 

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81182309