Shell开发rsync启动脚本

需求:实现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  ]

原文地址:http://blog.51cto.com/12643266/2299819

时间: 2024-11-12 10:33:54

Shell开发rsync启动脚本的相关文章

开发rsync启动脚本2

使用函数更加规范的开发rsync启动脚本 #!/bin/bash #chkconfig: 2345 20 80 #description: create by vincen . /etc/init.d/functions function usage(){ echo $"usage:$0 {start|stop|restart}" exit 1 } function start(){ rsync --daemon sleep 1 if [ `netstat -lntup|grep rs

企业级通过shell脚本开发MySQL启动脚本 案例

企业Shell面试题10:开发MySQL启动脚本说明MySQL启动命令为:/bin/sh mysqld_safe --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &停止命令为:mysqld_pid=`cat "$mysqld_pid_file_path"`if (kill -0 $mysqld_pid 2>/dev/null)  then    kill $mysqld_pid    sleep 2f

利用shell开发rsync服务启动脚本

利用shell函数开发rsync服务启动脚本,之前的不够专业 #!/bin/bash #chkconfig: 2345  20 80                       #这两行加入kconfig #description: Saves and restores system entropy  pool source /etc/init.d/functions    #调用标准的函数库 aa() {   echo "plz one canshu"   exit 5 } bb()

rsync启动脚本编写

需求:写一个rsync服务启动脚本 思路: 1.首先对脚本参数个数进行判断如果传参个数不等于1,则echo "Usage: $0 {start|restart|stop}" 2.定义函数service,通过case进行对脚本传参值的流程控制判断 3.启动服务采用命令rsync --daemon 4.停止服务采用kill -9 进程和删除rsync的PID文件 [[email protected] test]# cat /etc/init.d/rsyncd #!/bin/bash #rs

rsync启动脚本

#!/bin/bash #chkconfig: 2345 30 50 #Date:2017-6-29 #Author:xcn([email protected]) #version 1.0 PID="/var/run/rsync.pid" start_rsync(){ if [ -f $PID ]   then  echo "rsync is running" else  rsync --daemon echo "rsync is started"

用shell写ngin启动脚本

#!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions pid=/application/nginx/logs/nginx.pid nginx=/application/nginx/sbin/nginx RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK='\E[1;35m'

shell编写 ngin启动脚本

#!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions pid=/application/nginx/logs/nginx.pid nginx=/application/nginx/sbin/nginx RED_COLOR='\E[1;31m' GREEN_COLOR='\E[1;32m' YELLOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' PINK='\E[1;35m'

rsync 启动脚本

#!/bin/bash . /etc/init.d/functions pidfile="/var/run/rsyncd.pid" start_rsync="rsync --daemon --config=/etc/rsyncd.conf" status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep') function usage() { echo $"

python开发mongodb启动脚本

#!/usr/bin/env python #coding:utf-8 import os import sys def start(): os.system('/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf') def stop(): os.system('/usr/local/mongodb/bin/mongod --shutdown --dbpath /data/mongodb/db') def s