首页 > 代码库 > 启动和监控脚本
启动和监控脚本
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
启动和监控脚本