"/alidata/server/nginx/logs/nginx.pid" failed 简单分析

547 查看

基于阿里云,版本是 CentOS release 5.8 (Final)

先废话下,Nginx进程分为master进程和worker进程,nginx开始运行后
我们可以通过 ps aux|gerp nginx查看他的masterpid

Nginxstart之后会把它的master进程id写到*/nginx/logs/nginx.pid文件中。
stop之后就会把此pid kill掉,随之这个文件也会被删除。

此时你cat文件*/nginx/logs/nginx.pid得到的一串数字和上述 ps aux|gerp nginx 中的nginx master pid数据是一致的。
如下图:

那么nginx.pid文件写在哪个路径中,在哪里设置呢,当然可以在编译的时候指定(我没做过),这里我的是在conf文件中指定。

配置文件是在*/server/nginx/conf下,这里列出前四列

    user  www www;
    worker_processes  1;
    
    error_log  /alidata/log/nginx/error.log crit;
    pid        /alidata/server/nginx-1.2.5/logs/nginx.pid;

我们启动nginx的时候 大家都知道,可以如下命令:
/alidata/server/nginx/sbin/nginx -c /alidata/server/nginx/conf/nginx.conf
-c就是这里的conf文件了。


那么问题来了,我们在执行 nginx restart 或者strat stop中报类似错 :

nginx: [error] open() "/alidata/server/nginx/logs/nginx.pid" failed
(2: No such file or directory)

是哪里报错的呢,其实是在
/etc/init.d/nginx这个文件中,这里是nginx start stop reload restart 的命令源头。

这里我贴一下这个文件中的代码,这个文件可以自己修改或者优化。

改完之后执行 chkconfig --add /etc/init.d/nginx

如果chkconfig执行报错如下:

service nginx does not support chkconfig

可以在文件中头部加上下面这两句:

# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve

如果大概看懂了下面的脚本,就大概知道错误的源头在哪里啦。


#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by ruijie. at 2014.02.26
# if you find any errors on this scripts,please contact ruijie.
# and send mail to ruijie at gmail dot com.
#            ruijie.qiao@gmail.com
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# 
nginxd=/alidata/server/nginx/sbin/nginx
nginx_config=/alidata/server/nginx/conf/nginx.conf
nginx_pid=/alidata/server/nginx-1.2.5/logs/nginx.pid

RETVAL=0
prog="nginx"

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {
    
    if [ -e $nginx_pid ] && netstat -tunpl | grep nginx &> /dev/null;then
        echo "fuck nginx already running...."
        exit 1
    fi
        
    echo -n $"Starting $prog!"
    $nginxd -c ${nginx_config}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/nginx
    return $RETVAL
}


# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog!"
    $nginxd -s stop
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/nginx
}


# reload nginx service functions.
reload() {

    echo -n $"Reloading $prog!"
    #kill -HUP `cat ${nginx_pid}`
    $nginxd -s reload
    RETVAL=$?
    echo

}

# See how we were called.
case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

*)
        echo $"Usage: $prog {start|stop|restart|reload|help}"
        exit 1
esac

exit $RETVAL