Skip to content

Instantly share code, notes, and snippets.

@nikolavp
Last active December 14, 2015 21:38
Show Gist options
  • Save nikolavp/5152001 to your computer and use it in GitHub Desktop.
Save nikolavp/5152001 to your computer and use it in GitHub Desktop.
Logstash script
#! /bin/sh
### BEGIN INIT INFO
# Provides: logstash
### END INIT INFO
# Author: your name here <change email>
#
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="The logstash service"
NAME=logstash
SERVICE_USER="logstash"
DAEMON="java -cp /opt/logstash/logstash-dist logstash.runner agent -f /etc/logstash.conf"
DAEMON_ARGS=
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
# Init
VERBOSE=1
as_user() {
# Don't ask for password if we are logged in as reco
if [ "$(whoami)" != "reco" ];then
su - $SERVICE_USER -s /bin/bash -c "$1"
else
$1
fi
}
start_in_screen()
{
DAEMON_NAME=$1
DAEMON_EXEC="$2"
if as_user "screen -ls" | grep "\.$DAEMON_NAME" > /dev/null;then
echo $DAEMON_NAME is already running
return 1
else
echo Starting $DAEMON_NAME in screen
# Note the -- after the -s parameter. Some daemon execs contain parameters
as_user "screen -d -m -S $DAEMON_NAME -s -- $DAEMON_EXEC"
fi
}
do_to_screen()
{
DAEMON_NAME=$1
if ! as_user "screen -ls" | grep "\.$DAEMON_NAME" > /dev/null;then
echo $DAEMON_NAME wasn\'t started
return 1
fi
DAEMON_NAME=$1
WHAT=$2
if [ -z VERBOSE ];then
echo Typing $WHAT to $DAEMON_NAME
fi
as_user "screen -p 0 -S $DAEMON_NAME -X stuff \"$WHAT
\""
}
stop_in_screen()
{
DAEMON_NAME=$1
do_to_screen $DAEMON_NAME ""
}
#
# Function that starts the daemon/service
#
do_start()
{
start_in_screen $NAME "$DAEMON"
[ "$?" = 1 ] && return 1
}
#
# Function that stops the daemon/service
#
do_stop()
{
stop_in_screen $NAME
}
status_of_proc(){
DAEMON_NAME=$2
DAEMON_EXEC=$1
if as_user 'screen -ls' | grep "\.$DAEMON_NAME" > /dev/null;then
return 0
else
return 1
fi
}
case "$1" in
start)
[ "$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 "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
sleep 1
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|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment