#!/bin/bash APP_NAME=InsightServer PROG_NAME=$0 ACTION=$1 SERVER_PORT=20020 current_time=$(date +"%Y%m%d%H%M%S") LOG_FILE_NAME=logs/${current_time}.log usage() { echo "Usage: $PROG_NAME {start|stop|restart}" exit 2 } start_application() { echo "starting ${APP_NAME} process" nohup uvicorn run_server_async:app --host 0.0.0.0 --port ${SERVER_PORT} --workers 10 --reload > ${LOG_FILE_NAME} 2>&1 & echo "start success!!!" } stop_application() { pids=`lsof -i:${SERVER_PORT} | awk '{print$2}' | grep -v "PID"` if [[ ! $pids ]];then echo -e "\rno process" return fi echo "start stop ${APP_NAME} process" times=60 for e in $(seq 60);do sleep 1 COSTTIME=$(($times - $e )) pids=`lsof -i:${SERVER_PORT} | awk '{print$2}' | grep -v "PID"` if [[ $pids ]];then for pid in $pids; do echo "kill pid:${pid}" kill -9 $pid echo -e "\r -- stopping process lasts `expr $COSTTIME` seconds." done else echo -e "\r${APP_NAME} process has exited" break; fi done echo "" } start() { start_application } stop() { stop_application } case "$ACTION" in start) start ;; stop) stop ;; restart) stop start ;; *) usage ;; esac