在centos7上安装nginx之后,在/etc/init.d/nginx中有如下代码:
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/sinasrv2/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/sinasrv2/etc/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -TERM
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
在执行service nginx start命令的时候,程序会hung住,ctrl+c之后,发现nginx service已经启动;之后执行service nginx stop命令,返回ok,但是ps -ef | grep nginx的时候,发现nginx进程仍然存在,没有kill成功。
在configure的时候有指定 --pid-path=/var/run/nginx.pid参数,但是发现现在nginx.pid的目录是在/usr/local/sinasrv2/var/logs/nginx.pid。
nginx官方文档关于pid-path有如下解释,开始一度以为是configure设置的pid-path没有生效,被default了。
--pid-path=path — sets the name of an nginx.pid file that will store the process ID of the main process. After installation, the file name can always be changed in the nginx.conf configuration file using the pid directive. By default the file is named prefix/logs/nginx.pid.
同时也说了,可以在nginx.conf中指定nginx.pid的path,打开nginx.conf发现,果然是设置了nginx.pid。将之注释,手动kill掉nginx。再执行service nginx start; service nginx stop命令,一切正常了。
后续,进一步深入研究,nginx.pid的目录对于start和stop方法的影响。