Skip to content

Commit

Permalink
Better init script
Browse files Browse the repository at this point in the history
  • Loading branch information
John H committed Aug 12, 2016
1 parent 8b22b46 commit 627b2f5
Showing 1 changed file with 139 additions and 22 deletions.
161 changes: 139 additions & 22 deletions debian/on-tftp.init
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/sh

. /lib/lsb/init-functions

### BEGIN INIT INFO
# Provides: on-tftp
# Required-Start: $syslog $network mongodb rabbitmq-server
Expand All @@ -12,37 +10,156 @@
# Description: RackHD on-tftp service
### END INIT INFO

DESC="on-tftp"
NAME="on-tftp"
USER="root"
APP_DIR="/var/renasar/$NAME"
NODE_APP="index.js"
PID_DIR="/var/run"
PID_FILE="$PID_DIR/$NAME.pid"
LOG_DIR="/var/log/rackhd"
LOG_FILE="$LOG_DIR/$NAME.log"
NODE_EXEC=$(which node)

USAGE="Usage: $0 {start|stop|restart|status} [--force]"
FORCE_OP=false
RESTART=false

pid_file_exists() {
[ -f "$PID_FILE" ]
}

get_pid() {
echo "$(cat "$PID_FILE")"
}

is_running() {
PID=$(get_pid)
! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]
}

start_it() {
if [ ! -d $PID_DIR ]; then mkdir -p "$PID_DIR"; fi
chown $USER:$USER "$PID_DIR"
mkdir -p "$LOG_DIR"
chown $USER:$USER "$LOG_DIR"

echo "Starting $NAME ..."
/sbin/start-stop-daemon --background --start --quiet\
--make-pidfile --pidfile $PID_FILE \
-d $APP_DIR \
--startas /bin/bash -- -c "exec $NODE_EXEC $APP_DIR/$NODE_APP >> $LOG_FILE 2>&1"

echo "$NAME started with pid $(get_pid)"
}

stop_process() {
echo "Killing process $PID"
/sbin/start-stop-daemon -K -o --pidfile $PID_FILE
}

CONFIG=/etc/default/on-tftp
LOGFILE=/var/log/on-tftp.log
remove_pid_file() {
echo "Removing pid file"
rm -f "$PID_FILE"
}

do_start()
{
if [ -f "$CONFIG" ]
start_app() {
if pid_file_exists
then
cd /var/renasar/on-tftp
/usr/bin/nodejs /var/renasar/on-tftp/index.js >> "$LOGFILE" 2>&1 &
log_success_msg "Started on-tftp daemon"
if is_running
then
PID=$(get_pid)
echo "$NAME already running with pid $PID"
exit 1
else
echo "$NAME stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing start anyways"
remove_pid_file
start_it
fi
fi
else
log_failure_msg "Config file $CONFIG missing, not starting"
start_it
fi
}

stop_app() {
if pid_file_exists
then
if is_running
then
echo "Stopping $NAME ..."
stop_process
remove_pid_file
echo "$NAME stopped"
else
echo "$NAME already stopped, but pid file exists"
if [ $FORCE_OP = true ]
then
echo "Forcing stop anyways ..."
remove_pid_file
echo "$NAME stopped"
else
exit 1
fi
fi
else
echo "$NAME already stopped, pid file does not exist"
fi
}

status_app() {
if pid_file_exists
then
if is_running
then
PID=$(get_pid)
echo "$NAME running with pid $PID"
else
echo "$NAME stopped, but pid file exists"
fi
else
echo "$NAME stopped"
fi
}

ACTION=$1
case "$2" in
--force)
FORCE_OP=true
;;

case "$ACTION" in
"")
;;

*)
echo $USAGE
exit 1
;;
esac

case "$1" in
start)
do_start
;;
stop | restart | reload | force-reload | status)
# unimplemented
;;
start_app
;;

stop)
stop_app
;;

restart)
FORCE_OP=true
stop_app
start_app
;;

status)
status_app
;;

*)
logger "Unknown action \"$ACTION\""
;;
echo $USAGE
exit 1
;;
esac

exit 0

0 comments on commit 627b2f5

Please sign in to comment.