Skip to content

Instantly share code, notes, and snippets.

@pwm
Last active March 3, 2023 22:39
Show Gist options
  • Save pwm/d3260804b4ade0d81f29 to your computer and use it in GitHub Desktop.
Save pwm/d3260804b4ade0d81f29 to your computer and use it in GitHub Desktop.
openresty init script
#!/bin/bash
#
# chkconfig: 2345 55 25
# description: Openresty
# processname: nginx
# config: /usr/local/openresty/nginx/conf/nginx.conf
# pidfile: /usr/local/openresty/nginx/logs/nginx.pid
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
# Short-Description: Openresty init script
# Description: OpenResty is a full-fledged web application server bundling the standard Nginx core, 3rd-party Nginx modules, as well as most of their external dependencies.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Nginx Daemon"
NAME=nginx
PREFIX=/usr/local/openresty/nginx
DAEMON=${PREFIX}/sbin/${NAME}
CONF=${PREFIX}/conf/${NAME}.conf
PID=${PREFIX}/logs/${NAME}.pid
SCRIPT=/etc/init.d/${NAME}
if [ ! -x ${DAEMON} ]; then
echo -e "$(tput setaf 1) ${DAEMON} has no permission to run. $(tput sgr 0)"
exit 1
fi
if [ ! -f ${CONF} ]; then
echo -e "$(tput setaf 1) ${CONF} doesn't exist. $(tput sgr 0)"
exit 1
fi
check_pid() {
if [ -s ${PID} ]; then
PID_PROCESS=$(ps -p$(cat ${PID}) -o pid=)
else
PID_PROCESS=""
POS_PROCESS=$(ps aux | grep "nginx: master" | grep -v grep | head -n 1 | awk '{print $2}')
fi
}
remove_pid() {
if [ -f ${PID} ]; then
echo -e "$(tput setaf 1) ${PID} exists, ${DESC} probably crashed. $(tput sgr 0)"
echo -e "$(tput setaf 1) deleting ${PID} ... $(tput sgr 0)"
rm -f ${PID}
echo -e "$(tput setaf 1) ${PID} deleted. $(tput sgr 0)"
fi
}
check_pos_process() {
if [ ${#POS_PROCESS} -gt 0 ]; then
echo -e "$(tput setaf 1) WARNING: $(tput sgr 0)"
echo -e "$(tput setaf 1) There is a possible ${DESC} running with pid ${POS_PROCESS} but there is no pid file for it. $(tput sgr 0)"
exit 1
fi
}
do_start() {
check_pid
if [ ${#PID_PROCESS} -gt 0 ]; then
echo -e "$(tput setaf 1) ${DESC} is already running. $(tput sgr 0)"
echo -e "$(tput setaf 2) ${DESC} reopening ${CONF} ... $(tput sgr 0)"
${DAEMON} -s reopen -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} reopened. $(tput sgr 0)"
else
remove_pid
check_pos_process
echo -e "$(tput setaf 2) ${DESC} starting ${CONF} ... $(tput sgr 0)"
${DAEMON} -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} started. $(tput sgr 0)"
fi
}
do_stop() {
check_pid
if [ ${#PID_PROCESS} -eq 0 ]; then
echo -e "$(tput setaf 1) ${DESC} isn't running. $(tput sgr 0)"
remove_pid
check_pos_process
else
echo -e "$(tput setaf 2) ${DESC} stopping ${CONF} ... $(tput sgr 0)"
${DAEMON} -s stop -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} stopped. $(tput sgr 0)"
fi
}
do_quit() {
check_pid
if [ ${#PID_PROCESS} -eq 0 ]; then
echo -e "$(tput setaf 1) ${DESC} isn't running. $(tput sgr 0)"
remove_pid
check_pos_process
else
echo -e "$(tput setaf 2) ${DESC} quitting ${CONF} ... $(tput sgr 0)"
${DAEMON} -s quit -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} quitted. $(tput sgr 0)"
fi
}
do_reload() {
check_pid
if [ ${#PID_PROCESS} -eq 0 ]; then
echo -e "$(tput setaf 1) ${DESC} isn't running. $(tput sgr 0)"
remove_pid
check_pos_process
echo -e "$(tput setaf 2) ${DESC} starting ${CONF} ... $(tput sgr 0)"
${DAEMON} -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} started. $(tput sgr 0)"
else
echo -e "$(tput setaf 2) ${DESC} reloading ${CONF} ... $(tput sgr 0)"
${DAEMON} -s reload -c ${CONF}
sleep 1
echo -e "$(tput setaf 2) ${DESC} reloaded. $(tput sgr 0)"
fi
}
do_test() {
echo -e "$(tput setaf 2) ${DESC} testing ${CONF} ... $(tput sgr 0)"
${DAEMON} -t -c ${CONF}
}
do_info() {
${DAEMON} -V
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
quit)
do_quit
;;
reload)
do_reload
;;
restart)
do_stop
do_start
;;
test)
do_test
;;
info)
do_info
;;
*)
echo "$(tput setaf 2) Usage: $SCRIPT {start|stop|quit|reload|restart|test|info} $(tput sgr 0)"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment