启动脚本

 #!/bin/bash
# chkconfig: 2345 20 80
# description: Saves and restores system strapi
# start stop restart
source /root/.bash_profile
_proj=`echo $0 | awk -F '/' '{print $NF}'`
_PID=`/usr/bin/ps -ef | grep node | grep $_proj | grep start | awk  '{print $2}'`
_dir="/data/$_proj"
status(){
  if [ -n "$_PID" ]
  then
    echo "strapi is running ..."
  else
    echo "strapi is not running ..."
  fi
}
start() {
  if [ -n "$_PID" ];then
    echo "strapi is already running ..."
    exit 0
  fi
  #export NODE_ENV="staging"
  #export NODE_ENV="production"
  cd $_dir
  if [[ ! -d node_modules ]];then
     echo "node_modules does not exist. Install first."
     npm install && npm run build
  fi
  nohup /opt/node/bin/npm run start >output 2>1 &
  echo "Starting strapi daemon..."
  sleep 10
  #grep error output >/dev/null 2>&1
  /usr/bin/ps -ef | grep node | grep $_proj | grep start >/dev/null 2>&1
  if [[ $? -eq 1 ]];then
    echo "Starting strapi failed.Here's the strapi log..."
    cat output
    exit 1
  fi
  echo "Starting strapi ok"
}
stop() {
  if [ -n "$_PID" ]
  then
   echo "Stopping strapi daemon..."
   kill -9 $_PID
   _PID=""
   echo "Stopping strapi ok"
  else
   echo "strapi is not running ..."
  fi
}
restart(){
    stop
    sleep 1
    start
}
case "$1" in
start)
    start
;;
stop)
    stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
posted @ 2021-07-15 14:07  ianCloud  阅读(61)  评论(0编辑  收藏  举报