#! /bin/bash
# 基于 odp3.0的nginx,适用于odp3.0以下和odp3.0版本的nginx ,增加强制kill container里面的nginx的逻辑
# 20160129 升级chkconfig 增加创建log/webserver 目录部分
#
set -e
set -E
trap 'echo "Fail unexpectedly on ${BASH_SOURCE[0]}:$LINENO!" >&2' ERR
ODP_ROOT=$(readlink -f `dirname $BASH_SOURCE[0]`/..)
NGINX_HOME=${ODP_ROOT}/webserver
NGINX_BIN=$NGINX_HOME/sbin/nginx
NGINX_ARGS=' '
PID_FILE=${ODP_ROOT}/var/nginx.pid
NGINX_LOG="access_log error_log"
if [[ $USER == 'root' ]]; then
NGINX_ARGS='user work;'
fi
is_odp3() {
if [[ -e $ODP_ROOT/bin/odp_install ]];then
source $ODP_ROOT/bin/odp_install
GCONV_PATH=$ODP_GCONV_PATH
fi
}
not() {
if $@; then
return 1;
else
return 0;
fi
}
wait_for() {
local try=$1
shift
for ((;try>0;try--)); do
if $@ ; then
return 0
fi
echo -n .
sleep 1
done
return 1
}
process_exists() {
local pid=$1
local bin=$2
if [[ -d /proc/$pid ]]; then
local exe=`readlink -f /proc/$pid/exe`
if [[ $exe == $bin ]]; then
return 0
fi
# 对于ODP目录被移动的情况
if [[ ! -e $exe ]]; then
return 0
fi
fi
return 1
}
is_odp3 $NGINX_BIN
cd $NGINX_HOME/sbin
start() {
echo -n "Starting nginx: "
if [ ! -d "$ODP_ROOT/log/webserver" ]
then
mkdir -p "$ODP_ROOT/log/webserver"
fi
if [[ $USER == 'root' ]]; then
chmod 755 "$ODP_ROOT"
chmod 755 "$ODP_ROOT/log"
touch "$ODP_ROOT/log/access_log"
touch "$ODP_ROOT/log/error_log"
chmod 777 $ODP_ROOT/log/access_log
chmod 777 $ODP_ROOT/log/error_log
chmod 777 $ODP_ROOT/log/webserver
chmod 777 $ODP_ROOT/log/webserver/*
fi
if $NGINX_BIN -g "$NGINX_ARGS"</dev/null; then
echo "ok"
else
echo "fail"
exit 1
fi
}
stop() {
if [[ ! -f $PID_FILE ]]; then
mstop_forcibly
return
fi
PID=`head $PID_FILE`
if ! process_exists $PID $NGINX_BIN; then
rm $PID_FILE
mstop_forcibly
return
fi
echo -n "Stopping nginx: "
# when nginx.conf error, nginx -s stop will failed
kill $PID || true
sleep 1
mstop_forcibly
if wait_for 10 "not process_exists $PID $NGINX_BIN"; then
echo 'ok'
else
echo 'fail'
exit 1
fi
}
mstop_forcibly() {
PID=$$
ret=`grep -Eq ":/[0-9]+" "/proc/${PID}/cgroup" 2>/dev/null && echo 0 || echo 1`
if [[ $ret -eq 0 && -f "/usr/orp/bin/ps" ]]
then
## 判断是否是orp环境,且是container里面
echo "kill nginx forcibly"
for pid in `/usr/orp/bin/ps -eLf | grep "nginx" | grep -v grep | awk '{print $2}' | sort | uniq`
do
## 判断这些pid是否是nginx的进程
if process_exists $pid $NGINX_BIN; then
kill $pid 2>/dev/null || true
fi
done
fi
}
case "$1" in
start)
stop
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
if $NGINX_BIN -g "$NGINX_ARGS" -s reload; then
echo "reload ok, please check it youself";
else
echo "reload fail"
exit 1
fi
;;
chkconfig)
if [ ! -d "$ODP_ROOT/log/webserver" ]
then
mkdir -p "$ODP_ROOT/log/webserver"
fi
$NGINX_BIN -g "$NGINX_ARGS" -t
;;
*)
echo "Usage: $0 {start|stop|restart|chkconfig|reload}"
exit 1
esac