Skip to content

Instantly share code, notes, and snippets.

@zeuxisoo
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save zeuxisoo/9679722 to your computer and use it in GitHub Desktop.

Select an option

Save zeuxisoo/9679722 to your computer and use it in GitHub Desktop.
nginx.sh (start,stop,restart,reload,test,status)
#!/bin/sh
NGINX=/usr/local/nginx/sbin/nginx
CONFIG=/usr/local/nginx/conf/nginx.conf
DESC=nginx
COLOR_RESET="\033[0m"
COLOR_GREEN="\033[32m"
COLOR_RED="\033[31m"
message() {
retval=$1
if [ $retval -eq 0 ]; then
echo "${COLOR_GREEN}OK${COLOR_RESET}"
else
echo "${COLOR_RED}FAILED${COLOR_RESET}"
fi
}
start() {
echo -n "Starting $DESC: "
$NGINX -c $CONFIG
message $?
}
stop() {
echo -n "Stopping $DESC: "
$NGINX -s stop
message $?
}
restart() {
echo "Restarting $DESC: "
stop
sleep 1
start
message $?
}
reload() {
echo -n "Reloading $DESC: "
ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP
message $?
}
test() {
echo -n "Testing $DESC: "
if $NGINX -t -c $CONFIG >/dev/null 2>&1; then
message $?
else
$NGINX -t -c $CONFIG
exit $?
fi
}
status() {
ps aux | grep nginx
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
test)
test
;;
status)
status
;;
*)
echo "Usage: nginx.sh {start|stop|restart|test|reload|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment