nginx-amplify-agent/etc/init.d/amplify-agent

162 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx-amplify-agent
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Stop/start nginx-amplify-agent
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC=amplify-agent
NAME=amplify-agent
CONFFILE=/etc/amplify-agent/agent.conf
DAEMON=/usr/bin/nginx-amplify-agent.py
PIDFILE=/var/run/amplify-agent/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
USER=nginx
DEBUG_LOG="$2"
if [ -f "$CONFFILE" ]; then
CONFIG_AMPLIFY_USER=`grep -v '#' ${CONFFILE} | \
grep -A 10 -i '\[.*nginx.*\]' | \
grep -i 'user.*=' | \
awk -F= '{print $2}' | \
sed 's/ //g' | \
head -1`
NGINXCONF=`grep -v '#' ${CONFFILE} | \
grep -A 10 -i '\[.*nginx.*\]' | \
grep -i 'configfile.*=' | \
awk -F= '{print $2}' | \
sed 's/ //g' | \
head -1`
fi
if [ -z "$NGINXCONF" ]; then
NGINXCONF=/etc/nginx/nginx.conf
fi
if [ -f "$NGINXCONF" ]; then
CONFIG_NGINX_USER=`grep 'user[[:space:]]' ${NGINXCONF} | \
grep -v '[#].*user.*;' | \
grep -v '_user' | \
sed -n -e 's/.*\(user[[:space:]][[:space:]]*[^;]*\);.*/\1/p' | \
awk '{ print $2 }' | head -1`
fi
if [ -n "$CONFIG_AMPLIFY_USER" ]; then
USER=$CONFIG_AMPLIFY_USER
else
test -n "$CONFIG_NGINX_USER" && \
USER=$CONFIG_NGINX_USER
fi
[ -x $DAEMON ] || exit 0
DAEMON_ARGS="--config=$CONFFILE --pid=$PIDFILE"
. /lib/init/vars.sh
. /lib/lsb/init-functions
do_start()
{
mkdir -p /var/run/amplify-agent && chown $USER /var/run/amplify-agent
chown -f $USER /etc/amplify-agent/agent.conf
chown -f $USER /var/log/amplify-agent/agent.log
start-stop-daemon --start --chuid $USER --exec $DAEMON start -- $DAEMON_ARGS
RETVAL="$?"
if [ "$RETVAL" -ne 0 ]; then
return "$RETVAL"
fi
for i in 1 2 3 4 5; do
if [ -e "$PIDFILE" ]; then
return 0
fi
sleep 1
done
return 1
}
do_stop()
{
start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 --pidfile $PIDFILE --user $USER
RETVAL="$?"
rm -f $PIDFILE
return "$RETVAL"
}
do_configtest() {
$DAEMON configtest $DAEMON_ARGS
RETVAL="$?"
return $RETVAL
}
do_debug() {
mkdir -p /var/run/amplify-agent && chown $USER /var/run/amplify-agent
chown $USER /etc/amplify-agent/agent.conf
chown $USER `dirname $DEBUG_LOG`
start-stop-daemon --start --chuid $USER --exec $DAEMON debug -- $DAEMON_ARGS --log=$DEBUG_LOG
RETVAL="$?"
return $RETVAL
}
case "$1" in
start)
if status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" >/dev/null 2>&1; then
echo " * $NAME is already running" >&2
exit 0
fi
if [ -e "$PIDFILE" ]; then
rm -f "$PIDFILE"
fi
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
;;
configtest)
do_configtest
;;
debug)
do_debug
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|configtest|debug}" >&2
exit 3
;;
esac
exit $RETVAL