From 6b349114552e64e4f1e8fe33b1bf4f64afc0eac5 Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Mon, 21 Oct 2024 19:04:31 +0800 Subject: [PATCH] update script --- python/ppc_model/ppc_model_app.py | 1 + python/ppc_model/tools/start.sh | 63 ++++++++++++++++--------------- python/ppc_model/tools/stop.sh | 53 +++++++++++++++++++------- 3 files changed, 73 insertions(+), 44 deletions(-) diff --git a/python/ppc_model/ppc_model_app.py b/python/ppc_model/ppc_model_app.py index b1c0d7bc..95f2fe99 100644 --- a/python/ppc_model/ppc_model_app.py +++ b/python/ppc_model/ppc_model_app.py @@ -70,3 +70,4 @@ def register_task_handler(): print(message) components.logger().info(message) server.start() + print("Start model service successfully") diff --git a/python/ppc_model/tools/start.sh b/python/ppc_model/tools/start.sh index 477ac215..86cfd09f 100644 --- a/python/ppc_model/tools/start.sh +++ b/python/ppc_model/tools/start.sh @@ -1,36 +1,37 @@ #!/bin/bash +SHELL_FOLDER=$(cd $(dirname $0);pwd) +LOG_ERROR() { + content=${1} + echo -e "\033[31m[ERROR] ${content}\033[0m" +} -dirpath="$(cd "$(dirname "$0")" && pwd)" -cd $dirpath +LOG_INFO() { + content=${1} + echo -e "\033[32m[INFO] ${content}\033[0m" +} +binary_path=${SHELL_FOLDER}/ppc_model_app.py +cd ${SHELL_FOLDER} +node=$(basename ${SHELL_FOLDER}) +node_pid=$(ps aux|grep ${binary_path}|grep -v grep|awk '{print $2}') -# kill crypto process -crypto_pro_num=`ps -ef | grep /ppc/scripts | grep j- | grep -v 'grep' | awk '{print $2}' | wc -l` -for i in $( seq 1 $crypto_pro_num ) +if [ ! -z ${node_pid} ];then + echo " ${node} is running, pid is $node_pid." + exit 0 +else + nohup ${binary_path} > start.out 2>&1 & + sleep 1.5 +fi +try_times=4 +i=0 +while [ $i -lt ${try_times} ] do - crypto_pid=`ps -ef | grep /ppc/scripts | grep j- | grep -v 'grep' | awk '{print $2}' | awk 'NR==1{print}'` - kill -9 $crypto_pid + node_pid=$(ps aux|grep ${binary_path}|grep -v grep|awk '{print $2}') + success_flag=$(tail -n20 start.out | grep successfully) + if [[ ! -z ${node_pid} && ! -z "${success_flag}" ]];then + echo -e "\033[32m ${node} start successfully pid=${node_pid}\033[0m" + exit 0 + fi + sleep 0.5 + ((i=i+1)) done - -sleep 1 - -nohup python ppc_model_app.py > start.out 2>&1 & - -check_service() { - try_times=5 - i=0 - while [ -z `ps -ef | grep ${1} | grep python | grep -v grep | awk '{print $2}'` ]; do - sleep 1 - ((i = i + 1)) - if [ $i -lt ${try_times} ]; then - echo -e "\033[32m.\033[0m\c" - else - echo -e "\033[31m\nServer ${1} isn't running. \033[0m" - return - fi - done - - echo -e "\033[32mServer ${1} started \033[0m" -} - -sleep 5 -check_service ppc_model_app.py \ No newline at end of file +echo -e "\033[31m Exceed waiting time. Please try again to start ${node} \033[0m" \ No newline at end of file diff --git a/python/ppc_model/tools/stop.sh b/python/ppc_model/tools/stop.sh index 3b290668..937b3fd6 100644 --- a/python/ppc_model/tools/stop.sh +++ b/python/ppc_model/tools/stop.sh @@ -1,19 +1,46 @@ #!/bin/bash +SHELL_FOLDER=$(cd $(dirname $0);pwd) -dirpath="$(cd "$(dirname "$0")" && pwd)" -cd $dirpath +LOG_ERROR() { + content=${1} + echo -e "\033[31m[ERROR] ${content}\033[0m" +} -# kill crypto process -crypto_pro_num=`ps -ef | grep /ppc/scripts | grep j- | grep -v 'grep' | awk '{print $2}' | wc -l` -for i in $( seq 1 $crypto_pro_num ) -do - crypto_pid=`ps -ef | grep /ppc/scripts | grep j- | grep -v 'grep' | awk '{print $2}' | awk 'NR==1{print}'` - kill -9 $crypto_pid -done +LOG_INFO() { + content=${1} + echo -e "\033[32m[INFO] ${content}\033[0m" +} -sleep 1 +binary_path=${SHELL_FOLDER}/ppc_ppc_model_app.py +node=$(basename ${SHELL_FOLDER}) +node_pid=$(ps aux|grep ${binary_path}|grep -v grep|awk '{print $2}') +try_times=10 +i=0 +if [ -z ${node_pid} ];then + echo " ${node} isn't running." + exit 0 +fi -ppc_model_app_pid=`ps aux |grep ppc_model_app.py |grep -v grep |awk '{print $2}'` -kill -9 $ppc_model_app_pid +#Stop monitor here +dirs=($(ls -l ${SHELL_FOLDER} | awk '/^d/ {print $NF}')) +for dir in ${dirs[*]} +do + if [[ -f "${SHELL_FOLDER}/${dir}/node.mtail" && -f "${SHELL_FOLDER}/${dir}/stop_mtail_monitor.sh" ]];then + echo "try to start ${dir}" + bash ${SHELL_FOLDER}/${dir}/stop_mtail_monitor.sh & + fi +done -echo -e "\033[32mServer ppc_model_app.py killed. \033[0m" +[ ! -z ${node_pid} ] && kill ${node_pid} > /dev/null +while [ $i -lt ${try_times} ] +do + sleep 1 + node_pid=$(ps aux|grep ${binary_path}|grep -v grep|awk '{print $2}') + if [ -z ${node_pid} ];then + echo -e "\033[32m stop ${node} success.\033[0m" + exit 0 + fi + ((i=i+1)) +done +echo " Exceed maximum number of retries. Please try again to stop ${node}" +exit 1