15:开发Rsync服务启动脚本案例

[root@m01 ~]# rsn_count="ps -ef|grep 'rsync --d[a]emon'|wc -l"
[root@m01 ~]# echo ${rsn_count}
ps -ef|grep 'rsync --d[a]emon'|wc -l
[root@m01 ~]# eval ${rsn_count}
1

变量多次获取值得思路:

定义变量

每次执行的时候就执行   eval ${rsn_count}
每次获取的都是新值,

第二种思路:

一开始
 rsn_count=$(ps -ef|grep 'rsync --d[a]emon'|wc -l)

获取变量
然后再次获取的时候

执行命令ps -ef|grep 'rsync --d[a]emon'|wc -l 获取
[root@m01 20171207]# cat ryn_srv.sh 

#!/bin/bash
rsn_file="/usr/bin/rsync"
# 判断启动的进程数

[ -e /etc/init.d/functions ] && source /etc/init.d/functions || echo "/etc/init.d/functions 不存在"
[ -e ${rsn_file} ] || {
    echo "${rsn_file} 服务不存在"
    exit 11
}
args1=$1

function start() {
    if [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt 0 ]
    then
        echo "rsync 已经启动"
    
    else 

        ${rsn_file} --daemon
        sleep 1
        [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt 0 ] && action "rsync 启动成功"  /bin/true || echo "启动失败"

    fi

}

function stop() {

    if [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -gt 0 ]
        then
                killall rsync
                killall rsync &>/dev/null
        sleep 1        
        [ $(ps -ef|grep "rsync --d[a]emon"|wc -l) -lt 1 ] && action "rsync  关闭成功"  /bin/true || echo "关闭失败"

        
        else 

                echo "rsync 已经关闭"


        fi

    
}

function restart() {
    stop
    sleep 2
    start
}

case $args1 in
     start)
     start
     ;;
     stop)
     
     stop
     ;;
     
     restart)
     
     restart
     ;;
     
     *)
     echo "Usage  {start|stop|restart}"
     ;;
     
esac
原文地址:https://www.cnblogs.com/gaoyuechen/p/7999680.html