Start shell script for Nginx service in centos

Source: Internet
Author: User
Tags stop script centos

Nginx service startup script

 

The code is as follows: Copy code

#! /Bin/sh

# Chkconfig: 2345 55 25

# Description: Startup script for nginx webserver on Debian. Place in/etc/init. d and

# Run 'UPDATE-rc. d-f nginx defaults', or use the appropriate command on your

# Distro. For CentOS/Redhat run: 'chkconfig-add nginx'

 

### BEGIN INIT INFO

# Provides: nginx

# Required-Start: $ all

# Required-Stop: $ all

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: starts the nginx web server

# Description: starts nginx using start-stop-daemon

### END INIT INFO

 

# Author: licess

# Website: http://lnmp.org

 

PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

NAME = nginx

NGINX_BIN =/usr/local/nginx/sbin/$ NAME

CONFIGFILE =/usr/local/nginx/conf/$ NAME. conf

PIDFILE =/usr/local/nginx/logs/$ NAME. pid

 

Case "$1" in

Start)

Echo-n "Starting $ NAME... "

 

If netstat-tnpl | grep-q nginx; then

Echo "$ NAME (pid 'pidof $ name') already running ."

Exit 1

Fi

 

$ NGINX_BIN-c $ CONFIGFILE

 

If ["$ ?" ! = 0]; then

Echo "failed"

Exit 1

Else

Echo "done"

Fi

;;

 

Stop)

Echo-n "Stoping $ NAME... "

 

If! Netstat-tnpl | grep-q nginx; then

Echo "$ NAME is not running ."

Exit 1

Fi

 

$ NGINX_BIN-s stop

 

If ["$ ?" ! = 0]; then

Echo "failed. Use force-quit"

Exit 1

Else

Echo "done"

Fi

;;

 

Status)

If netstat-tnpl | grep-q nginx; then

PID = 'pidof nginx'

Echo "$ NAME (pid $ PID) is running... "

Else

Echo "$ NAME is stopped"

Exit 0

Fi

;;

 

Force-quit)

Echo-n "Terminating $ NAME... "

 

If! Netstat-tnpl | grep-q nginx; then

Echo "$ NAME is not running ."

Exit 1

Fi

 

Kill 'pidof $ name'

 

If ["$ ?" ! = 0]; then

Echo "failed"

Exit 1

Else

Echo "done"

Fi

;;

 

Restart)

$0 stop

Sleep 1

$0 start

;;

 

Reload)

Echo-n "Reload service $ NAME... "

 

If netstat-tnpl | grep-q nginx; then

$ NGINX_BIN-s reload

Echo "done"

Else

Echo "$ NAME is not running, can't reload ."

Exit 1

Fi

;;

 

Configtest)

Echo-n "Test $ NAME configure files... "

 

$ NGINX_BIN-t

;;

 

*)

Echo "Usage: $0 {start | stop | force-quit | restart | reload | status | configtest }"

Exit 1

;;


Example 2

Save the following script as an nginx file and put it in/etc/init. d/nginx

Then you can use

The code is as follows: Copy code
/Etc/init. d/nginx start command to start nginx
/Etc/init. d/nginx stop command to stop nginx
/Etc/init. d/nginx restart command restart nginx

Start nginx automatically at startup,

If you need to start the service, after saving the/etc/init. d/nginx file,

Run the following command:

The code is as follows: Copy code

Chkconfig -- add ningx

Chkconfig -- level nginx 2345 on

You can also download the nginx Start Stop script directly.

------------------- The split line contains the script content. You need to change the red font -------------------------------------------

The code is as follows: Copy code

#! /Bin/sh

PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC = "nginx daemon"
NAME = nginx
DAEMON =/usr/local/nginx/sbin/$ NAME
CONFIGFILE =/usr/local/nginx/conf/$ NAME. conf
PIDFILE =/usr/local/nginx/logs/$ NAME. pid
SCRIPTNAME =/etc/init. d/$ NAME

Set-e
[-X "$ DAEMON"] | exit 0

Do_start (){
$ DAEMON-c $ CONFIGFILE | echo-n "nginx already running"
}

Do_stop (){
Kill-INT cat $ PIDFILE | echo-n "nginx not running"
}

Do_reload (){
Kill-HUP cat $ PIDFILE | echo-n "nginx can't reload"
}

Case "$1" in
Start)
Echo-n "Starting $ DESC: $ NAME"
Do_start
Echo "."
;;
Stop)
Echo-n "Stopping $ DESC: $ NAME"
Do_stop
Echo "."
;;
Reload | graceful)
Echo-n "Reloading $ DESC configuration ..."
Do_reload
Echo "."
;;
Restart)
Echo-n "Restarting $ DESC: $ NAME"
Do_stop
Do_start
Echo "."
;;
*)
Echo "Usage: $ SCRIPTNAME {start | stop | reload | restart}"> & 2
Exit 3
;;
Esac

Exit 0

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.