shell 启动python并实现监控

#!/bin/bash

app_path=/home/python_data/flask_service.py

date=$(date "+%Y-%m-%d  %H:%M:%S")
ID=`ps -ef | grep flask_service | grep python | awk '{print $2}'`
input=$1
if [ ! -n "$input" ]
 then
  echo -e "\033[43;35m please append start|stop|restart|check \033[0m \n"
else
 case $input in
 start)
  if [ -z "$ID" ]
  then 
   nohup python $app_path &>> app.log &
   echo "start app successful"
   echo "$date start app successful" >> app.log
  else
   echo "app already started"
  fi
 ;;
 stop)
  if [ -z "$ID" ]
   then
   echo "$date NO MATCH APP STARTED" >> app.log
   echo "NO MATCH APP STARTED"
  else
   kill -9 $ID & >> app.log
                 echo "$date stop app successful" >> app.log
   echo "stop app successful"
  fi
 ;;
 restart)
  if [ -z "$ID" ]
  
 then
   nohup python $app_path &>> app.log &
   echo "restart app successful"
   echo "$date restart app successful" >> app.log
  else
   kill -9 $ID & >> app.log
   nohup python $app_path &>> app.log &
   echo "stop old app and restart restart new app successful"
   echo "$date stop old app and restart new app successful" >> app.log
  fi
 ;;
 check)
  if [ -z "$ID" ]
  then
   echo "APP is stopped"
  else
   echo "APP is running and process ID is $ID"
  fi
 ;;
 *)
  echo -e "\033[43;35m please append start|stop|restart|check \033[0m \n"  
 esac

fi

你可能感兴趣的:(ssh,linux)