Nginx boot script _nginx in Ubuntu system

Source: Internet
Author: User
Tags syslog nginx server

Copy Code code as follows:

#! /bin/sh
### BEGIN INIT INFO
# Provides:nginx
# Required-start: $remote _fs $syslog
# required-stop: $remote _fs $syslog
# Default-start:2 3 4 5
# default-stop:0 1 6
# Short-description:nginx init.d Dash script for Ubuntu or other *nix.
# Description:nginx init.d Dash script for Ubuntu or other *nix.
### End INIT INFO
#------------------------------------------------------------------------------
# nginx-this Debian almquist Shell (dash) script, starts and stops the Nginx
# Daemon for Ubuntu and other *nix releases.
#
# Description:nginx is a HTTP (s) server, HTTP (s) reverse \
# Proxy and IMAP/POP3 proxy server. This \
# Script would manage the initiation of the
# Server and it ' s process state.
#
# Processname:nginx
# config:/usr/local/nginx/conf/nginx.conf
# Pidfile:/usr/local/nginx/logs/nginx.pid
# Provides:nginx
#
# Author:jason Giedymin
# <jason.giedymin at Gmail.com>.
#
# version:3.5.1 11-nov-2013 jason.giedymin at gmail.com
# Notes:nginx init.d Dash script for Ubuntu.
# tested With:ubuntu 13.10, nginx-1.4.3
#
# This script ' s project home is:
# Http://github.com/JasonGiedymin/nginx-init-ubuntu
#
#------------------------------------------------------------------------------
# MIT X11 License
#------------------------------------------------------------------------------
#
# Copyright (c) 2008-2013 Jason giedymin, http://jasongiedymin.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# A copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# Distribute, sublicense, and/or sell copies of the Software, and to
# Permit persons to whom the Software are furnished to does so, subject to
# The following conditions:
#
# The above copyright notice and this permission notice shall to be
# included in all copies or substantial portions of the Software.
#
# The SOFTWARE is provided ' as is ', without WARRANTY of any KIND,
# EXPRESS OR implied, including BUT not LIMITED to the warranties of
# merchantability, FITNESS for A particular purpose and
# noninfringement. In NO EVENT SHALL the AUTHORS OR COPYRIGHT holders is
# liable for all CLAIM, damages OR other liability, WHETHER in a ACTION
# of CONTRACT, TORT or otherwise, arising from, out of OR in CONNECTION
# with the SOFTWARE or the ' use ' or ' dealings ' in the SOFTWARE.
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------
Lsb_func=/lib/lsb/init-functions

# Test that init functions exists
Test-r $LSB _func | | {
echo "$0:cannot find $LSB _func! Script exiting. " 1>&2
Exit 5
}

. $LSB _func

#------------------------------------------------------------------------------
# consts
#------------------------------------------------------------------------------
Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Daemon=/usr/local/nginx/sbin/nginx

Ps= "Nginx"
Pidname= "Nginx" #lets your do $PS-slave
pidfile= $PIDNAME. pid #pid File
Pidspath=/usr/local/nginx/logs #default PID location, should change it

description= "Nginx Server ..."

Runas=root #user to run as

Script_ok=0 #ala Error Codes
Script_error=1 #ala ERROR Codes
True=1 #boolean
False=0 #boolean

Lockfile=/var/lock/subsys/nginx
Nginx_conf_file= "/usr/local/nginx/conf/nginx.conf"

#------------------------------------------------------------------------------
# Simple Tests
#------------------------------------------------------------------------------

# Test If Nginx is a file and executable
Test-x $DAEMON | | {
echo "$0:you don t have permissions to execute nginx." 1>&2
Exit 4
}

# Include Nginx Defaults if available
if [-f/etc/default/nginx]; Then
. /etc/default/nginx
Fi

#set exit condition
#set-E

#------------------------------------------------------------------------------
# functions
#------------------------------------------------------------------------------

Setfileperms () {
If [f $PIDSPATH/$PIDFILE]; Then
chmod $PIDSPATH/$PIDFILE
Fi
}

Configtest () {
$DAEMON-T-C $NGINX _conf_file
}

Getpscount () {
Return ' Pgrep-f $PS | Wc-l '
}

IsRunning () {
if [$]; Then
Pidof_daemon $
Pid=$?

If [$PID-gt 0]; Then
Return 1
Else
return 0
Fi
Else
Pidof_daemon
Pid=$?

If [$PID-gt 0]; Then
Return 1
Else
return 0
Fi
Fi
}

#courtesy of PHP-FPM
Wait_for_pid () {
Try=0

    while test $try-LT does
        case "$" in
 & nbsp;          ' created ')
             if [-F "$]"; Then
                try= '
                Break
             fi
            ;

' Removed ')
if [!-f ' $ ']; Then
Try= '
Break
Fi

Esac

try= ' expr $try + 1 '
Sleep 1
Done
}

Status () {
IsRunning
Isalive=$?

if ["${isalive}"-eq $TRUE]; Then
Log_warning_msg "$DESCRIPTION found running with processes: ' Pidof $PS '"
Rc=0
Else
Log_warning_msg "$DESCRIPTION is not running."
Rc=3
Fi

Return
}

Removepidfile () {
    if [$]; then
        if [F $]; N
            rm-f $
         fi
    else
        #Do default removal
        If [f $PIDSPATH/$PIDFILE]; then
             rm-f $PIDSPATH/$PIDFILE
        fi
    fi
}

Start () {
Log_daemon_msg "Starting $DESCRIPTION"

IsRunning
Isalive=$?

if ["${isalive}"-eq $TRUE]; Then
Log_end_msg $SCRIPT _error
Rc=0
Else
Start-stop-daemon--start--quiet--chuid \
$RUNAS--pidfile $PIDSPATH/$PIDFILE--exec $DAEMON \
---C $NGINX _conf_file
Setfileperms
Log_end_msg $SCRIPT _OK
Rc=0
Fi

Return
}

Stop () {
Log_daemon_msg "Stopping $DESCRIPTION"

IsRunning
Isalive=$?

if ["${isalive}"-eq $TRUE]; Then
Start-stop-daemon--stop--quiet--pidfile $PIDSPATH/$PIDFILE

Wait_for_pid ' removed ' $PIDSPATH/$PIDFILE

If [-N "$try"]; Then
Log_end_msg $SCRIPT _error
Rc=0 # LSB states 1, but under status it is 2 (which are more prescriptive). Deferring to standard.
Else
Removepidfile
Log_end_msg $SCRIPT _OK
Rc=0
Fi
Else
Log_end_msg $SCRIPT _error
Rc=7
Fi

Return
}

Reload () {
Configtest | | Return $?

Log_daemon_msg "reloading (via HUP) $DESCRIPTION"

IsRunning

If [$-eq $TRUE]; Then
Kill-hup ' cat $PIDSPATH/$PIDFILE '
Log_end_msg $SCRIPT _OK
Rc=0
Else
Log_end_msg $SCRIPT _error
Rc=7
Fi

Return
}

Quietupgrade () {
Log_daemon_msg "peforming Quiet Upgrade $DESCRIPTION"

IsRunning
Isalive=$?

if ["${isalive}"-eq $TRUE]; Then
KILL-USR2 ' cat $PIDSPATH/$PIDFILE '
Kill-winch ' Cat $PIDSPATH/$PIDFILE. Oldbin '

IsRunning
Isalive=$?

if ["${isalive}"-eq $TRUE]; Then
Kill-quit ' Cat $PIDSPATH/$PIDFILE. Oldbin '
Wait_for_pid ' removed ' $PIDSPATH/$PIDFILE. Oldbin
Removepidfile $PIDSPATH/$PIDFILE. Oldbin

Log_end_msg $SCRIPT _OK
Rc=0
Else
Log_end_msg $SCRIPT _error

Log_daemon_msg "error! Reverting back to original $DESCRIPTION "

Kill-hup ' cat $PIDSPATH/$PIDFILE '
Kill-term ' Cat $PIDSPATH/$PIDFILE. Oldbin '
Kill-quit ' Cat $PIDSPATH/$PIDFILE. Oldbin '

Wait_for_pid ' removed ' $PIDSPATH/$PIDFILE. Oldbin
Removepidfile $PIDSPATH/$PIDFILE. Oldbin

Log_end_msg $SCRIPT _OK
Rc=0
Fi
Else
Log_end_msg $SCRIPT _error
Rc=7
Fi

Return
}

Terminate () {
Log_daemon_msg "Force terminating (via KILL) $DESCRIPTION"

pids= ' pidof $PS ' | | True

[-e $PIDSPATH/$PIDFILE] && pids2= ' cat $PIDSPATH/$PIDFILE '

For i in $PIDS; Todo
If ["$i" = "$PIDS 2"]; Then
Kill $i
Wait_for_pid ' removed ' $PIDSPATH/$PIDFILE
Removepidfile
Fi
Done

Log_end_msg $SCRIPT _OK
Rc=0
}

Destroy () {
Log_daemon_msg "Force terminating and may include self (via KILLALL) $DESCRIPTION"
Killall $PS-Q >>/dev/null 2>&1
Log_end_msg $SCRIPT _OK
Rc=0
}

Pidof_daemon () {
pids= ' pidof $PS ' | | True

[-e $PIDSPATH/$PIDFILE] && pids2= ' cat $PIDSPATH/$PIDFILE '

For i in $PIDS; Todo
If ["$i" = "$PIDS 2"]; Then
Return 1
Fi
Done

return 0
}

Action= "$"
Case "$" in
Start
Start

Stop
Stop

Restart|force-reload)
Stop
# if [$RC-ne 0]; Then
# Script_exit
# fi
Sleep 1
Start

Reload
$

Status
Status

Configtest)
$

Quietupgrade)
$

Terminate
$

Destroy
$

*)
Fullpath=/etc/init.d/$PS
echo "Usage: $FULLPATH {Start|stop|restart|force-reload|status|configtest|quietupgrade|terminate|destroy}"
echo "The ' destroy ' command should only is used as a last resort."
Exit 3

Esac

Exit $RC

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.