Add nginx to system services and control it using chkconfig

Source: Internet
Author: User
& Nbsp; dedicated wait 1. write a script named nginx #! /Bin/sh # nginx-thisscriptstartsandstopsthenginxdaemon # chkconfig:-8515 # description: NginxisanHTTP (S) server, HTTP (S) reverse # proxyandIMAP/

Dedicated waiting

1. write a script named nginx
#! /Bin/sh
#
# Nginx-this script starts and stops the nginx daemon
#
# Chkconfig:-85 15
# Description: Nginx is an HTTP (S) server, HTTP (S) reverse
# Proxy and IMAP/POP3 proxy server
# Processname: nginx
# Config:/etc/nginx. conf
# Config:/etc/sysconfig/nginx
# Pidfile:/var/run/nginx. pid

# 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/nginx/sbin/nginx"
Prog = $ (basename $ nginx)

NGINX_CONF_FILE = "/usr/local/nginx/conf/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-QUIT
Retval =$?
Echo
[$ Retval-eq 0] & rm-f $ lockfile
Return $ retval
Killall-9 nginx
}

Restart (){
Configtest | return $?
Stop
Sleep 1
Start
}

Reload (){
Configtest | return $?
Echo-n $ "Reloading $ prog :"
Killproc $ nginx-HUP
RETVAL =$?
Echo
}

Force_reload (){
Restart
}

Configtest (){
$ Nginx-t-c $ NGINX_CONF_FILE
}

Rh_status (){
Status $ prog
}

Rh_status_q (){
Rh_status>/dev/null 2> & 1
}

Case "$1" in
Start)
Rh_status_q & exit 0
$1
;;
Stop)
Rh_status_q | exit 0
$1
;;
Restart | configtest)
$1
;;
Reload)
Rh_status_q | exit 7
$1
;;
Force-reload)
Force_reload
;;
Status)
Rh_status
;;
Condrestart | try-restart)
Rh_status_q | exit 0
;;
*)
Echo $ "Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest }"
Exit 2
Esac
2. add chkconfig
[Root @ example ~] # Cp nginx/etc/init. d/
[Root @ example ~] # Chmod 755/etc/init. d/nginx
[Root @ example ~] # Chkconfig -- add nginx

3. start, stop, and restart nginx
[Root @ example ~] # Service nginx start
[Root @ example ~] # Service nginx stop
[Root @ example ~] # Service nginx reload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.