当前位置:网站首页>Flash operation and maintenance script (running for a long time)

Flash operation and maintenance script (running for a long time)

2022-07-06 09:56:00 east

flask Every time the project is opened, it will automatically close .
I heard that brother afar (QQ25008793) Write a script for me .... An awesome batch ..

Run in the background and Shell Exit the program and still run the command :

nohup app.py & In the actual process , We usually use scripts , The other is to use docker k8s Automatic arrangement .

Write a script for you .

Upload the script to this directory and you can use it , The way

sh ./start.sh start

start.sh The code is as follows :

function start(){
    
  if [ ! -d 'venv' ]; then
    python3 -m venv venv
  fi

  source venv/bin/activate

  # requirements.txt
  if [ ! -e 'requirements.txt' ]; then
    echo 'error: "requirements.txt" file not found!'
    exit
  fi

  pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  nohup python app.py & echo $! > app.pid
  echo "run app.py pid:$(cat app.pid)"
}

function stop() {
    
  if [ -e 'app.pid' ]; then
    pid=$(cat app.pid)
    rm -rf 'app.pid'
    for line in $(ps -ef|grep python|awk '{print $2}')
    do
      if [ $pid = $line ]; then
        echo "kill: $pid"
        kill $pid
      fi
    done
  fi
}

function status() {
    
    if [ -e 'app.id' ]; then
      pid=$(cat 'app.id')
      for line in $(ps -ef|grep python|awk '{print $2}')
      do
        if [ $pid = $line ]; then
          echo "app.py ok $pid"
        fi
      done
    fi
}

function main(){
    
  command=$1
  case command in
  'start')
    start
  ;;
  'stop')
    stop
  ;;
  'restart')
    stop
    start
  ;;
  'status')
    status
  ;;
  *)
    echo '-------------------------- The list of commands --------------------------'
    echo '| start  start-up  |'
    echo '| stop  stop it  |'
    echo '| restart  restart  |'
    echo '| status  state  |'
    echo '-----------------------------------------------------------'
  ;;
}

# --------------------------

main $*
原网站

版权声明
本文为[east]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060905199339.html