zoukankan      html  css  js  c++  java
  • 启动和监控脚本

    start.sh

    #!/bin/sh

    #export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
    LD_LIBRARY_PATH=../lib:$LIBPATH:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
    LIBPATH=../lib:$LIBPATH
    export LIBPATH

    PROGRAMNAME="agent"
    COMMANDLINE="./${PROGRAMNAME} -m"
    LOGFILE="../log/monitor.log"

    LOGPATH=`dirname $LOGFILE`
    if [ ! -d $LOGPATH ]
    then
    mkdir -p $LOGPATH
    fi

    logtime=`date +"%Y-%m-%d %H:%M:%S"`
    PID=`ps -ef|grep -v grep|grep "${COMMANDLINE}"|awk '{print $2;}'`;

    if [ "$PID" = "" ]
    then
    echo "[${logtime}] ${COMMANDLINE} -> start" | tee -a ${LOGFILE}
    nohup ${COMMANDLINE} 4 >>${LOGFILE} 2>&1 &
    sleep 1
    PID=`ps -ef|grep -v grep|grep "${COMMANDLINE}"|awk '{print $2;}'`
    if [ "$PID" = "" ]
    then
    echo "[${logtime}] ${COMMANDLINE} -> start failed, please check logfile[${LOGFILE}]!" | tee -a ${LOGFILE}
    fi
    else
    echo "[${logtime}] ${COMMANDLINE} -> exist" #| tee -a ${LOGFILE}
    fi

    stop.sh

    #!/bin/sh

    PROGRAMNAME="agent"
    COMMANDLINE="./${PROGRAMNAME} -m"
    LOGFILE="../log/monitor.log"

    logtime=`date +"%Y-%m-%d %H:%M:%S"`
    echo "[${logtime}] ${COMMANDLINE} -> stop" | tee -a ${LOGFILE}
    ps -ef |grep -v grep | grep "${COMMANDLINE}"|awk '{print "kill -9 ",$2}'|sh 2>>${LOGFILE}

    PID=`ps -ef|grep -v grep|grep "${COMMANDLINE}"|awk '{print $2;}'`
    if [ "$PID" != "" ]
    then
    echo "[${logtime}] ${COMMANDLINE} -> stop failed, please check logfile[${LOGFILE}]!" | tee -a ${LOGFILE}
    fi

    monitor.sh

    #!/bin/sh

    cd
    if [ -f .bash_profile ]
    then
    . ./.bash_profile
    elif [ -f .profile ]
    then
    . ./.profile
    elif [ -f .bashrc ]
    then
    . ./.bashrc
    fi

    #parm1 is program dir
    cd ${1}

    LOGFILE="../log/monitor.log"
    #create dir
    LOGPATH=`dirname $LOGFILE`
    if [ ! -d $LOGPATH ]
    then
    mkdir -p $LOGPATH
    fi

    NOWTIME=`date '+%Y%m%d%H%M%S'`
    NEWAGENT="${NOWTIME}agent"
    PACKAGENAME=""
    PACKAGEPATH=""
    exist_package() {
    if [ ! -d ../resource ]
    then
    echo "${NOWTIME} not resource dir"
    return 1
    fi
    if [ ! -d ../resource/package ]
    then
    echo "${NOWTIME} not resource package dir"
    return 1
    fi
    PACKAGENAME=`ls ../resource/package| grep '.tar.gz$' |grep -w agent`
    PACKAGEPATH=`echo "${PACKAGENAME}" | sed s/.tar.gz//g`
    NUM=`ls ../resource/package| grep '.tar.gz$' |grep -w agent | wc -l`
    if [ ${NUM} -eq 1 ]
    then
    return 0
    else
    return 1
    fi
    }

    rm_mv_package(){
    mv ../resource/package/${PACKAGEPATH}.tar ../resource/package/${PACKAGEPATH}.tar_bak
    rm -rf ../resource/package/${PACKAGEPATH}
    }

    tar_package(){
    cd ../resource/package
    gzip -df ${PACKAGENAME}
    tar -xf ${PACKAGEPATH}.tar
    if [ $? -eq 0 ]
    then
    return 0
    else
    return 1
    fi
    }

    update_agent(){
    echo "${NOWTIME} update_agent begin!" | tee -a ${LOGFILE}
    if tar_package;then
    cd $1
    if [ -f "../resource/package/${PACKAGEPATH}/agent/bin/agent" ]
    then
    sh stop.sh
    mv agent ${NEWAGENT}
    cp ../resource/package/${PACKAGEPATH}/agent/bin/agent ./agent
    chmod +x agent
    sh start.sh
    sleep 1
    pid=`ps -ef | grep "agent -m" | grep -v "grep"|awk '{print $2}'`
    if [ $pid ]
    then
    echo "${NOWTIME} ${PACKAGENAME} agent start $pid!" | tee -a ${LOGFILE}
    rm_mv_package
    return 0
    else
    echo "${NOWTIME} ${PACKAGENAME} agent start failed!" | tee -a ${LOGFILE}
    rm_mv_package
    mv ${NEWAGENT} agent
    sh start.sh
    return 1
    fi
    else
    echo "${NOWTIME} ${PACKAGENAME} have not agent!" | tee -a ${LOGFILE}
    rm_mv_package
    return 1
    fi
    else
    echo "${NOWTIME} tar ${PACKAGENAME} failed!" | tee -a ${LOGFILE}
    rm_mv_package
    return 1
    fi
    }

    #run
    if exist_package; then
    if update_agent $1; then
    echo "${NOWTIME} ${PACKAGENAME} update_agent success!" | tee -a ${LOGFILE}
    else
    echo "${NOWTIME} ${PACKAGENAME} update_agent failed!" | tee -a ${LOGFILE}
    fi
    else
    sh start.sh
    fi

  • 相关阅读:
    UVA 11149.Power of Matrix-矩阵快速幂倍增
    51nod 1137.矩阵乘法-矩阵乘法
    HDU 4920.Matrix multiplication-矩阵乘法
    HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    HDU 6235.Permutation (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
    POJ 2226.Muddy Fields-二分图最大匹配(最小点覆盖)
    POJ 3041.Asteroids-Hungary(匈牙利算法)
    HDU 2063.过山车-Hungary(匈牙利算法)
    Codeforces 832 B. Petya and Exam-字符串匹配
    HDU 6038.Function-数学+思维 (2017 Multi-University Training Contest
  • 原文地址:https://www.cnblogs.com/wcc331902579/p/5912557.html
Copyright © 2011-2022 走看看