美文网首页
通用shell启动/终止进程脚本

通用shell启动/终止进程脚本

作者: falc0n | 来源:发表于2020-04-18 14:00 被阅读0次
#!/bin/env/bash

SHELL_DIR=$(cd "$(dirname "$0")";pwd)
opt="${@:2}"
echo "shell dir: $SHELL_DIR, arguments:$opt"
 
psid=
 
checkpid() {
   psid=`ps -ef | grep 'php-fpm' | grep -v 'grep' | grep -v 'sh' | awk '{print $2}' | tr '\n' ' '`
   echo "psid: $psid"
}
 
start() {
     echo "starting..."
     checkpid
      if [ ${#psid} -ne 0 ]; then
         echo "================================"
         echo "warn: php-fpm already started! (pid=$psid)"
         echo "================================"
      else
         echo "============ Start ============="
         echo "starting php-fpm ..."
         php-fpm $opt
         checkpid
         if [ ${#psid} -ne 0 ]; then
            echo "php-fpm started successfully!"
         else
            echo "php-fpm started failed!"
         fi
     fi
}
 
stop() {
   checkpid
    if [ ${#psid} -ne 0 ]; then
       echo "killing pid -> "$psid
       kill -9 $psid
         checkpid
         if [ ${#psid} -ne 0 ]; then
            echo "php-fpm stopped failed!"
         else
            echo "php-fpm stopped successfully!"
         fi
   else
      echo "================================"
      echo "warn: php-fpm is not running!"
      echo "================================"
   fi
 
}
 
status() {
   checkpid
   if [ ${#psid} -ne 0 ];  then
      echo "php-fpm is running!"
   else
      echo "php-fpm is not running!"
   fi
}
 
echo "running $1 command as $USER"
 
case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'status')
     status
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|status}"
     exit 1
esac
exit 0

相关文章

网友评论

      本文标题:通用shell启动/终止进程脚本

      本文链接:https://www.haomeiwen.com/subject/dngvvhtx.html