[TOOL] php-dws 启动关闭控制脚本(php-dwsctl)

把下面的代码内容复制保存为 php-dwsctl 放在 /usr/bin 或 /usr/local/bin 之类的 PATH 路径中,那么就可以简单的调用以下命令来控制:
1. 启动 php-dwsctl start
2. 关闭 php-dwsctl stop
3. 重开 php-dwsctl restart

#!/bin/sh
# php-dwsctl <start|stop|restart>
#
php_dws=/usr/bin/php-dws
php_tmp_dir=/tmp

php_dws_children=16
php_dws_max_requests=512
php_user=nobody
php_group=

php_sock=$php_tmp_dir/php-dws.sock
pid_file=$php_tmp_dir/php-dws.pid

case "$1" in
  start)
    # check binary
    if [ ! -x $php_dws ] ; then
      echo "ERROR: Executable binary php-dws not found: $php_dws"
      exit 1
    fi
    # tmp dir
    if [ ! -d $php_tmp_dir ] ; then
      mkdir -m 0777 $php_tmp_dir
    fi
    if [ ! -d $php_tmp_dir ] || [ ! -w $php_tmp_dir ] ; then
      echo "ERROR: Temporary dir not exists or cannt' be written"
      exit 1
    fi
    # check upload & session
    php_up_dir=`$php_dws -i | grep upload_tmp_dir | cut -d ' ' -f 3`
    php_sess_dir=`$php_dws -i | grep session.save_path | cut -d ' ' -f 3`
    if [ "$php_up_dir" != "" ] && [ ! -d $php_up_dir ] ; then
      if ! mkdir -m 0777 $php_up_dir ; then
        echo "WARNING: upload_tmp_dir not exists"
      fi
    fi
    if [ "$php_sess_dir" != "" ] && [ ! -d $php_sess_dir ] ; then
      if ! mkdir -m 0777 $php_sess_dir ; then
        echo "WARNING: upload_sess_dir not exists"
      fi
    fi
    # check running
    if test -f $pid_file ; then
      pid=`cat $pid_file`
      if kill -0 $pid > /dev/null 2>&1 ; then
        echo "[ERROR] PHP-dws is running (pid=$pid)"
        exit 1
      fi
    fi
    # server options
    php_options=""
    if [ "$php_sock" != "/tmp/php-dws.sock" ] ; then
      php_options="$php_options -b$php_sock"
    fi
    if [ "$php_dws_children" != "16" ] ; then
      php_options="$php_options -C$php_dws_children"
    fi
    if [ "$php_dws_max_requests" != "512" ] ; then
      php_options="$php_options -C$php_dws_max_requests"
    fi
    run_uid=`id -u`
    if [ "$run_uid" = "0" ] ; then
      if [ "$php_user" != "" ] ; then
        php_options="$php_options -U$php_user"
      fi
      if [ "$php_group" != "" ] ; then
        php_options="$php_options -G$php_group"
      fi      
    fi
    php_options="$php_options -P$pid_file" 
    # starting & check
    echo -n "[INFO] Starting php-dws ... "
    $php_dws $php_options
    pid=`cat $pid_file`
    if kill -0 $pid > /dev/null 2>&1 ; then
      echo "OK(pid=$pid)"
    else
      echo "FAILED"
      exit 1
    fi
  ;;      
  stop)
    if test -f $pid_file ; then
      pid=`cat $pid_file`
      rm -f $pid_file
      if kill -TERM $pid > /dev/null 2>&1 ; then
        echo -n "[INFO] Killing running PHP-dws(pid=$pid) ..."
        while kill -0 $pid > /dev/null 2>&1 ; do
          echo -n "."
          sleep 1
        done
        echo " DONE"
        exit 0
      fi
    fi
    echo "[ERROR] PHP-dws may have not started?"  
    exit 1
  ;;
  restart)
    $0 stop
    $0 start
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
  ;;
esac
exit 0

发表评论

电子邮件地址不会被公开。 必填项已用*标注