Last active
August 29, 2015 14:00
-
-
Save combusean/11312673 to your computer and use it in GitHub Desktop.
nginx Red Hat init.d script beta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# nginx - this script starts and stops the nginx daemon for Red Hat-based distributions | |
# | |
# chkconfig: - 85 15 | |
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ | |
# proxy and IMAP/POP3 proxy server | |
# processname: nginx | |
# config: /etc/nginx/nginx.conf | |
# config: /etc/sysconfig/nginx | |
# pidfile: /var/run/nginx.pid | |
# | |
# comments, bugfixes to [email protected] | |
# 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_CONF_FILE="/etc/nginx/nginx.conf" | |
NGINX_PID_FILE="/var/run/nginx.pid" | |
NGINX_BIN="/usr/sbin/nginx" | |
NGINX_WORKER_TIMEOUT=30 | |
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx | |
nginx=$NGINX_BIN | |
prog=$(basename $nginx) | |
lockfile=/var/lock/subsys/nginx | |
RETVAL=0 | |
make_dirs() { | |
# make required directories | |
local user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` | |
if [ -z "`grep $user /etc/passwd`" ]; then | |
useradd -M -s /bin/nologin $user | |
fi | |
local options=`$nginx -V 2>&1 | grep 'configure arguments:'` | |
for opt in $options; do | |
if [ `echo $opt | grep '.*-temp-path'` ]; then | |
value=`echo $opt | cut -d "=" -f 2` | |
if [ ! -d "$value" ]; then | |
# echo "creating" $value | |
mkdir -p $value && chown -R $user $value | |
fi | |
fi | |
done | |
} | |
start() { | |
config_test || exit 6 | |
echo -n "Starting $prog: " | |
if ! [ -x $nginx ]; then | |
failure $"$nginx not executable" | |
RETVAL=5 | |
else | |
if check_stopped; then | |
daemon $nginx -c $NGINX_CONF_FILE | |
RETVAL=$? | |
else | |
failure $"$prog already started" | |
RETVAL=1 | |
fi | |
fi | |
[ $RETVAL -eq 0 ] && touch $lockfile | |
echo | |
return $RETVAL | |
} | |
stop() { | |
echo -n $"Stopping $prog: " | |
if check_started; then | |
local pid=$(<"$NGINX_PID_FILE") | |
kill -QUIT $pid | |
for (( i=0; i<=$NGINX_WORKER_TIMEOUT; i++ )); do | |
if check_stopped; then | |
RETVAL=0 | |
rm -f $lockfile | |
success | |
break; | |
fi | |
echo -n "." | |
sleep 1 | |
done | |
if [ $RETVAL -ne 0 ]; then | |
failure $"$prog workers are still around after $NGINX_WORKER_TIMEOUT seconds" | |
RETVAL=1 | |
fi | |
else | |
failure $"$prog not started" | |
RETVAL=1 | |
fi | |
echo | |
return $RETVAL | |
} | |
force_stop() { | |
echo -n $"Sending SIGKILL to $prog and workers: " | |
if check_started; then | |
pid=$(<"$NGINX_PID_FILE") | |
pkill -9 -g $pid | |
sleep 3 | |
if check_stopped; then | |
rm -f $NGINX_PID_FILE | |
rm -f $lockfile | |
success | |
RETVAL=0 | |
else | |
failure $"nginx survived SIGKILL" | |
RETVAL=1 | |
fi | |
else | |
failure "$prog not started" | |
RETVAL=1 | |
fi | |
echo | |
return $RETVAL | |
} | |
restart() { | |
config_test || exit 6 | |
stop || exit 1 | |
start || exit 1 | |
} | |
force_restart() { | |
force_stop | |
start || exit 1 | |
} | |
reload() { | |
config_test || exit 6 | |
echo -n $"Reloading $prog: " | |
killproc $nginx -HUP | |
RETVAL=$? | |
[ $RETVAL -eq 0 ] && rm -f $lockfile | |
echo | |
return $RETVAL | |
} | |
config_test() { | |
echo -n $"Checking $prog configuration: " | |
local config_output=$($nginx -t -c $NGINX_CONF_FILE 2>&1) | |
RETVAL=$? | |
if [ $RETVAL -eq 0 ]; then | |
success | |
echo | |
else | |
failure $"nginx -t -c $NGINX_CONF_FILE failed" | |
echo | |
echo $config_output | |
fi | |
return $RETVAL | |
} | |
check_stopped() { | |
! status $prog >/dev/null | |
RETVAL=$? | |
return $RETVAL | |
} | |
check_started() { | |
status $prog >/dev/null | |
RETVAL=$? | |
return $RETVAL | |
} | |
case "$1" in | |
start|stop|restart|reload) | |
$1 ;; | |
status) | |
status $prog ;; | |
force-restart|force-reload) | |
force_restart ;; | |
force-stop) | |
force_stop ;; | |
condrestart|try-restart) | |
check_started || exit 0 ;; | |
configtest) | |
config_test ;; | |
*) | |
echo $"Usage: $0 {start|stop|force-stop|status|restart|force-restart|condrestart|try-restart|reload|force-reload|configtest}" | |
exit 2 | |
esac | |
exit $RETVAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment