linux下所有服务的启动脚本大多大同不异。nginx也不例外。因为我在生产环境下的linux都是通过源码包安装的(有些linux版本里已经集成了nginx)。而每次启动都跑到安装路径里去启动我本人倒是习惯了。不过部门里那帮写代码的家伙们不习惯。所以只好也像其他服务一样,搞个启动脚本写到/etc/init.d里面了。脚本内容如下,比一般的流传版本多了个test的测试配置文件的功能。

 1#!/bin/bash
 2# nginx Startup script for the Nginx HTTP Server
 3# chkconfig: - 85 15
 4# processname: nginx
 5# pidfile: /var/run/nginx.pid
 6# config: /usr/local/nginx/conf/nginx.conf
 7nginxd=/App/nginx/sbin/nginx
 8nginx_config=/App/nginx/conf/nginx.conf
 9nginx_pid=/App/nginx/logs/nginx.pid
10RETVAL=0
11prog="nginx"
12# Source function library.
13. /etc/rc.d/init.d/functions
14# Source networking configuration.
15. /etc/sysconfig/network
16# Check that networking is up.
17[ ${NETWORKING} = "no" ] && exit 0
18[ -x $nginxd ] || exit 0
19# Start nginx daemons functions.
20start() {
21if [ -e $nginx_pid ];then
22   echo "nginx already running...."
23   exit 1
24fi
25   echo -n $"Starting $prog: "
26   daemon $nginxd -c ${nginx_config}
27   RETVAL=$?
28   echo
29   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
30   return $RETVAL
31}
32# Stop nginx daemons functions.
33stop() {
34        echo -n $"Stopping $prog: "
35        killproc $nginxd
36        RETVAL=$?
37        echo
38        [ $RETVAL = 0 ] && /bin/rm -f /var/lock/subsys/nginx /App/nginx/logs/nginx.pid
39}
40# test nginx daemons functions.
41test() {
42        echo "test $nginx_config configure file "
43        $nginxd -t
44}
45# reload nginx service functions.
46reload() {
47    echo -n $"Reloading $prog: "
48    #kill -HUP `cat ${nginx_pid}`
49    killproc $nginxd -HUP
50    RETVAL=$?
51    echo
52}
53# See how we were called.
54case "$1" in
55start)
56        start
57        ;;
58stop)
59        stop
60        ;;
61test)
62        test
63        ;;
64reload)
65        reload
66        ;;
67restart)
68        stop
69        start
70        ;;
71status)
72        status $prog
73        RETVAL=$?
74        ;;
75*)
76        echo $"Usage: $prog {start|stop|test|restart|reload|status|help}"
77        exit 1
78esac
79exit $RETVAL