Bash scripts examples : Graylog-collector init script

dell

Introduction

This script is a part of our on-going series “Bash script example“.

Here is a graylog-collector init script I wrote today, I still have issue regarding the “status” function, but otherwise everything work as expected. Note that you need to add a graylog-collector user and that user must be within the adm group (this will allow him to read system logs).

This script runs on a Debian Wheezy box.

 

More “bash scripts examples” posts

 

1 The code

#! /bin/sh
### BEGIN INIT INFO
# Provides:          graylog-collector
# Required-Start:    $network $named $remote_fs $syslog
# Required-Stop:     $network $named $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Graylog Collector
# Description:       Graylog's open source log shipper
### END INIT INFO

# Process name ( For display )
NAME="graylog-collector"
# Daemon name, where is the actual executable
DAEMON="/etc/graylog-collector/bin/graylog-collector"
# DAEMON args
DAEMON_OPT="run -f /etc/graylog-collector/config/collector.conf"
# User to run the deamon
DAEMON_USER="graylog-collector" # <- DO NOT DO THIS, the custom user would not be allowed to read system log
# pid file for the daemon
PIDDIR="/var/run/$NAME"
PIDFILE="$PIDDIR/$NAME.pid"


PATH="/sbin:/bin:/usr/sbin:/usr/bin"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions




d_start () {
        log_daemon_msg "Starting system $NAME Daemon"
        if [ ! -e $PIDDIR ] ; then
                mkdir $PIDDIR
                chown ${DAEMON_USER}:${DAEMON_USER} $PIDDIR
        fi
        start-stop-daemon --background --start \
                --user $DAEMON_USER \
                --chuid $DAEMON_USER \
                --make-pidfile \
                --pidfile $PIDFILE \
                --startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPT >> /var/log/graylog-collector/console.log 2>&1" || return 2
        sleep 2
        log_end_msg $?
}

d_stop () {
        log_daemon_msg "Stopping system $NAME Daemon"
        start-stop-daemon --stop --retry 5 --quiet --user $DAEMON_USER
        rm -f $PIDFILE
        log_end_msg $?
}

case "$1" in

        start|stop)
                d_${1}
                ;;

        restart|reload|force-reload)
                        d_stop
                        d_start
                ;;

        force-stop)
                d_stop
                killall -q $NAME || true
                sleep 2
                killall -q -9 $NAME || true
                ;;

        status)
                status_of_proc "${PIDFILE}" "$DAEMON" "$NAME" && exit 0 || exit $?
                ;;
        *)
                echo "Usage: /etc/init.d/$NAME {start|stop|force-stop|restart|reload|force-reload|status}"
                exit 1
                ;;
esac
exit 0

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.