carlessanagustin
9/4/2017 - 2:37 PM

Template SysVinit scripts $ service mongodb_exporter start|stop $ /etc/init.d/mongodb_exporter start|stop $ update-rc.d mongodb_exporter def

Template SysVinit scripts $ service mongodb_exporter start|stop $ /etc/init.d/mongodb_exporter start|stop $ update-rc.d mongodb_exporter defaults

#!/bin/bash

export USER='ubuntu'

export DAEMON_PATH=/usr/bin
export DAEMON=node_exporter
export DAEMONOPTS=""

export NAME=node_exporter
export PIDFILE=/var/run/$NAME.pid
export LOG_NAME=prometheus
export LOG_PATH=/var/log/$LOG_NAME

case "$1" in
start)
  if [ ! -d t ]; then mkdir $LOG_PATH && chown $USER:$USER $LOG_PATH; fi
	printf "%-50s" "Starting $NAME..."
	PID=`su $USER -c '$DAEMON_PATH/$DAEMON $DAEMONOPTS >> $LOG_PATH/$NAME.log 2>&1 & echo $!'`
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            su $USER -c 'echo "Stopping service..." >> $LOG_PATH/$NAME.log'
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;

restart)
  	$0 stop
  	$0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac