加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Shell开发rsync启动脚本

发布时间:2020-12-15 23:22:23 所属栏目:安全 来源:网络整理
导读:需求:实现shell脚本对rsync的start|stop|restart rsync pid所在路径:/var/run/rsyncd.pid rsync启动命令:rsync --daemon rsync进程停止命令:pkill rsync 文中通过判断rsync pid所在路径和进程状态来实现rsync的start|stop|restart 1)脚本演示 [[email?p
需求:实现shell脚本对rsync的start|stop|restart
rsync pid所在路径:/var/run/rsyncd.pid
rsync启动命令:rsync --daemon
rsync进程停止命令:pkill rsync

文中通过判断rsync pid所在路径和进程状态来实现rsync的start|stop|restart

1)脚本演示

[[email?protected] scripts]# cat rsync.sh 
#!/bin/sh
#This is the launch script for rsync.
#-------------------------------------
#Author:jason
#QQ:760966297
#mile:[email?protected]
#------------------------------------- 
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1
PID_PATH=/var/run/rsyncd.pid

fun_usage(){
        echo "Usage:$0 {start|stop|restart}"
 exit 2 
}

fun_start(){
if [ ! -e $PID_PATH ]; then
        rsync --daemon
        PORT=`netstat -lntup |grep rsync|wc -l`
        [ $PORT -eq 2 ]&& action "Rsync is startup successful." /bin/true ||action "Rsync start failed." /bin/false;

else
        action "Rsync is running." /bin/false
fi
}

fun_stop(){
if [ ! -e $PID_PATH ];then
        action "Rsync is not run." /bin/false
else
        while true
        do
                pkill rsync
                sleep 2
                PORT=`netstat -lntup |grep rsync|wc -l`
                [ $PORT -eq 0 ]&&action "Rsync is stop successful." /bin/true && break
        done
fi
}

fun_restart(){
if [ ! -e $PID_PATH ];then
        action "Rsync is not run." /bin/false
        exit 2
 else
        fun_stop >/dev/null
        sleep 2 
        fun_start >/dev/null
        PORT=`netstat -lntup |grep rsync|wc -l`
        [ $PORT -eq 2 ]&&action "Rsync is startup successful." /bin/true ||  action "Rsync is restart failed." /bin/false
fi
}

case $1 in
   start)
         fun_start
         ;;
    stop)
         fun_stop
         ;;
 restart)
         fun_restart
         ;;
       *)
         fun_usage
         ;;
esac

2)脚本测试

#测试fun_usage函数
[[email?protected] scripts]# sh rsync.sh  #<==运行脚本,脚本后面没有加参数的提示
Usage:rsync.sh {start|stop|restart}

#测试start
[[email?protected] scripts]# sh rsync.sh start #<==启动rsync
Rsync is startup successful.                               [  OK  ]
[[email?protected] scripts]# netstat -lntup |grep 873
tcp        0      0 0.0.0.0:873       0.0.0.0:*       LISTEN      98926/rsync         
tcp        0      0 :::873                   :::*             LISTEN      98926/rsync     

#测试stop
[[email?protected] scripts]# sh rsync.sh stop #<==关闭rsync
^[[ARsync is stop successful.                              [  OK  ]
[[email?protected] scripts]# netstat -lntup |grep 873

#测试restart
[[email?protected] scripts]# sh rsync.sh restart #<==重启rsync,但是rsync进程未开启,提示报错
Rsync is not run.                                          [FAILED]

[[email?protected] scripts]# sh rsync.sh start  #<==先启动rsync
Rsync is startup successful.                               [  OK  ]

[[email?protected] scripts]# sh rsync.sh restart  #<==然后再测试restart
Rsync is startup successful.                               [  OK  ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读