一、 启动脚本模板:符合幂等性
如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动
#!/bin/bash
instancename=
# check is instance running
PID=`ps -ef | $instancename | grep -v grep `
if [ ! -z "$PID" ]; then
echo "instance $instancename is running."
exit 0
fi
# start instance
# TODO: start cmd
# chenk whether instance be running by url or key word in logfile,choose one or check url
url=
loop=60
count=0
while $count < 60
do
curl $url && exit 0
sleep 1
count=$(($count + 1))
done
if [ $count -ge 60 ];then
echo "[ERROR]: Timeout ,failed."
exit 1
fi
echo "[INFO]: Instance $instancename started."
# or check key word in logfile
keyword= xxx
logfile=
orgLineNum=`wc -1 $logfile | cut -d " " -f1`
loop=60
count=0
while $count < 60
do
endLineNum=`wc -1 $logfile | cut -d " " -f1`
deltaLine=$(($endLineNum - $orgLineNum))
tail -n $deltaLine $logfile | sed /$keyword/ && break
$orgLineNum=$endLineNum
sleep 1
done
if [ $count -ge 60 ];then
echo "[ERROR]: Timeout , failed."
exit 1
fi
echo "[INFO]: Instance $instancename started."
二、停止脚本,符合幂等性
可以重复调用
#!/bin/bash
instancename=
#check is instance running
PID=`ps -ef | grep $instancename | grep -v grep `
if [ -z "$PID" ];then
echo "instance $instancename is not running."
exit 0
fi
# stop instance
# TODO : stop cmd
# if stop cmd failed ,may kill or exit with error
#or kill
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
echo "stop cmd failed , try to kill."
kill $PID
fi
# if kill failed ,may kill -9
if [ ! -z "$PID" ];then
echo "kill process failed, try to kill -9."
kill -9 $PID
fi
# or exit with error
PID=`ps -ef | grep $instancename | grep -v grep `
if [ ! -z "$PID" ];then
echo "stop cmd failed."
exit 1
fi