> CentOS > CentOS入门 >

Nginx启动脚本

#!/bin/sh 
# 
# Startup script for the Nginx 
# chkconfig: - 88 63 
# description: Nginx is a free,open-source,high-performance HTTP Server and reverse proxy. 
# program:/usr/local/nginx/sbin/nginx 
# config:/usr/local/nginx/conf/nginx.conf 
# pidfile:/usr/local/nginx/logs/nginx.pid 
 
# Synopsis: 
#        nginx [--help] [--version] {start|stop|restart|reload|status|update} 
 
 
# Define variable 
nginx=/usr/local/nginx/sbin/nginx 
pidfile=/usr/local/nginx/logs/nginx.pid 
PROGRAM=`basename $0` 
VERSION=1.0 
# Functions 
usage(){ 
    echo "Usage: $PROGRAM [--help] [--version] {start|stop|restart|reload|status|update}" 
} 
 
version(){ 
    echo "Version:$VERSION" 
} 
 
start(){ 
if [ -e $pidfile ] 
   then 
    echo "Nginx already running..." 
   else 
    echo -e "Starting Nginx:\t\t\t\t\t\t\t\c" 
    /usr/local/nginx/sbin/nginx 
    echo -e "[ \c" 
    echo -e "\033[0;32mOK\033[0m\c" 
    echo -e " ]\c" 
    echo -e "\r" 
fi 
} 
 
stop(){ 
if [ -e $pidfile ] 
   then 
    echo -e "Stopping Nginx:\t\t\t\t\t\t\t\c" 
    kill -TERM `cat ${pidfile}` 
    echo -e "[ \c" 
    echo -e "\033[0;32mOK\033[0m\c" 
    echo -e " ]\c" 
    echo -e "\r" 
   else 
    echo "Nginx already stopped..." 
fi 
} 
 
reload(){ 
if [ -e $pidfile ] 
   then 
    echo -e "Reloading Nginx:\t\t\t\t\t\t\c" 
    kill -HUP `cat ${pidfile}` 
    echo -e "[ \c" 
    echo -e "\033[0;32mOK\033[0m\c" 
    echo -e " ]\c" 
    echo -e "\r" 
   else 
    echo "Nginx is not running..." 
fi 
} 
 
status(){ 
    if [ -e $pidfile ] 
       then 
        PID=`cat $pidfile` 
        echo  "Nginx (pid $PID) is running..." 
       else 
        echo  "Nginx is stopped" 
    fi 
} 
 
update(){ 
if [ -e $pidfile ] 
   then 
    echo -e "Updateing Nginx:\t\t\t\t\t\t\c" 
    kill -USR2 `cat ${pidfile}` 
    echo -e "[ \c" 
    echo -e "\033[0;32mOK\033[0m\c" 
    echo -e " ]\c" 
    echo -e "\r" 
   else 
    echo "Nginx is not running..." 
fi 
} 
if [ $# -gt 0 ] 
   then 
    case $1 in 
        start) 
            start 
            ;; 
        stop) 
            stop 
            ;; 
        restart) 
            stop 
            start 
            ;; 
        reload) 
            reload 
            ;; 
        status) 
            status 
            ;; 
        update) 
            update 
            ;; 
        --help) 
            usage 
            ;; 
        --version) 
            version 
            ;; 
        *) 
            usage 
    esac 
   else 
    usage 
fi 
如果你的系统使用的是CentOS或RedHat的系统,可以通过chkconfig将其设置为开机启动项。
[root@centos6 ~] chkconfig  --add  nginx
[root@centos6 ~] chkconfig  nginx  on

(责任编辑:IT)