服务启动脚本

经常使用脚本启动项目,故此完善脚本记录一下,欢迎小白抱团!

#!/bin/bash

if [ -n "$2" ];
then
  echo $addr
else
  addr="0.0.0.0:9000"
fi;

function status() {
    keyword=$1;
    pid=$(ps -ef | grep ${keyword} | awk 'NR==1{print $2}');
    child=$(ps -ef | grep ${keyword} | awk 'NR==2{print $3}');
    if [[ -n "$pid" ]] && [[ -n "$child" ]] && [[ "$pid" = "$child" ]];
    then
      echo "the status of server:$keyword is started!";
      return 1;
    else
      echo "the status of server:$keyword is stopped";
      return 0;
    fi;
}

function start(){
  keyword=$1;

  index=0
  # 失败重启次数
  times=10
  until [ ! $index -lt $times ]
  do
    status $keyword;
    if [ "$?" = 0 ];
    then
      echo "[server:$keyword] is starting...";
      if [ "$keyword" = "celery" ];
      then
        nohup python3 manage.py celery -A kw.celery_app:app worker -B -l info > celery.log 2>&1 &
#        python manage.py celery -A kw.celery_app:app worker -B -l info -D;
      else
        gunicorn -w 4 kw.wsgi:application -b ${addr} --reload -D;
      fi
    fi;

    status $keyword;
    if [ "$?" != 0 ];
    then
      echo "[server:$keyword] is started.";
      break
    else
        sleep 10
        index=`expr $index + 1`
    fi;

  done

};

function stop(){
  keyword=$1;
  status $keyword;
  if [ "$?" = 1 ];
  then
    echo "[server:$keyword] is stopping...";
    pid=$(ps -ef | grep ${keyword} | awk 'NR==1{print $2}')
    kill ${pid}
    echo "[server:$keyword] is stopped";
  fi;
};

action=$1
case $action in
   start)
     start $addr;
     start "celery";
     ;;
   stop)
    stop $addr;
    stop "celery";
     ;;
   restart)
     stop $addr;
     stop "celery";
     start $addr;
     start "celery";
     ;;
   status)
     status $addr;
     status "celery";
     ;;
   *) echo "$0 {start|stop|restart|status}"
     exit 4
     ;;
esac
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值