Skip to content

Instantly share code, notes, and snippets.

@jippi jippi/rc.d forked from vvuksan/gist:988765
Created Jul 5, 2011

Embed
What would you like to do?
Logstash Init script
update-rc.d logstash-shipper defaults
update-rc.d logstash-reader defaults
#! /bin/sh
### BEGIN INIT INFO
# Provides: logstash-shipper
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
. /lib/lsb/init-functions
mode="reader"
name="logstash-$mode"
logstash_bin="/usr/bin/java -- -jar /opt/logstash/logstash.jar"
logstash_conf="/etc/logstash/$mode.conf"
logstash_log="/var/log/logstash/$name.log"
pid_file="/var/run/$name.pid"
NICE_LEVEL="-n 19"
start () {
command="/usr/bin/nice ${NICE_LEVEL} ${logstash_bin} agent -f $logstash_conf --log ${logstash_log}"
log_daemon_msg "Starting $mode" "$name"
if start-stop-daemon --start --quiet --oknodo --pidfile "$pid_file" -b -m --exec $command; then
log_end_msg 0
else
log_end_msg 1
fi
}
stop () {
start-stop-daemon --stop --quiet --oknodo --pidfile "$pid_file"
}
status () {
status_of_proc -p $pid_file "" "$name"
}
case $1 in
start)
if status; then exit 0; fi
start
;;
stop)
stop
;;
reload)
stop
start
;;
restart)
stop
start
;;
status)
status && exit 0 || exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
;;
esac
exit 0
#! /bin/sh
### BEGIN INIT INFO
# Provides: logstash-shipper
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
. /lib/lsb/init-functions
mode="shipper"
name="logstash-$mode"
logstash_bin="/usr/bin/java -- -jar /opt/logstash/logstash.jar"
logstash_conf="/etc/logstash/$mode.conf"
logstash_log="/var/log/logstash/$name.log"
pid_file="/var/run/$name.pid"
NICE_LEVEL="-n 19"
start () {
command="/usr/bin/nice ${NICE_LEVEL} ${logstash_bin} agent -f $logstash_conf --log ${logstash_log}"
log_daemon_msg "Starting $mode" "$name"
if start-stop-daemon --start --quiet --oknodo --pidfile "$pid_file" -b -m --exec $command; then
log_end_msg 0
else
log_end_msg 1
fi
}
stop () {
start-stop-daemon --stop --quiet --oknodo --pidfile "$pid_file"
}
status () {
status_of_proc -p $pid_file "" "$name"
}
case $1 in
start)
if status; then exit 0; fi
start
;;
stop)
stop
;;
reload)
stop
start
;;
restart)
stop
start
;;
status)
status && exit 0 || exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
;;
esac
exit 0
@optikfluffel

This comment has been minimized.

Copy link

commented Aug 24, 2012

you should really add this to the logstash cookbook :)

@munhitsu

This comment has been minimized.

Copy link

commented Dec 17, 2012

Thanks!

@eugenetaranov

This comment has been minimized.

Copy link

commented Jan 15, 2013

reload of config file is working fine:

reload)
pkill -HUP -u $LOGSTASH_USER
;;

@dotalbot

This comment has been minimized.

Copy link

commented Apr 10, 2013

Very useful. Thank you!

@vjeantet

This comment has been minimized.

Copy link

commented May 5, 2013

Thanks a lot for this Gist !

How to start logstash as a "logstash" user ?

@zuloo

This comment has been minimized.

Copy link

commented Dec 13, 2013

just add a --chuid logstash:logstash option, like:

  start () {
      log_daemon_msg "Starting $mode" "$name"
      if start-stop-daemon --start --quiet --oknodo --pidfile "$pid_file" --chuid logstash:logstash -b -m --exec $command; then
          log_end_msg 0
      else
          log_end_msg 1
      fi
  }
@AndyCCChang

This comment has been minimized.

Copy link

commented Mar 28, 2014

Hello,
How should I use this script?
Should I create a file named logstash-reader and logstash-shipper?
Where should I put the file, logstash-reader and logstash-shipper?

then
excute
update-rc.d logstash-shipper defaults
update-rc.d logstash-reader defaults
commands in anywhere?

After that?
What command I should type to start the logstash?
Something like logstash-reader start?

Thank you very much.

Andy

@OverlordAlex

This comment has been minimized.

Copy link

commented Jul 2, 2014

@AndyCCChang, I know this may be a little late, but for future users

To use this script:
create logstash-reader in /etc/init.d/
put the reader code from above into that file
make it executable with: chmod +x logstash-reader
run: update-rc.d logstash-reader defaults

Do the same for the shipper on the other machine (typically, depending on your setup. For example, I'm only running the reader)
[These insctructions were taken from StackOverflow, look here for more info: http://stackoverflow.com/questions/7221757/run-automatically-program-on-startup-under-linux-ubuntu]

logstash-reader will then be run at startup
you can also use the commands
service logstash-reader status/start/restart/stop
(these should be pretty self-explanatory)

It also looks like the logstash cookbook is down :(

@zanderle

This comment has been minimized.

Copy link

commented Aug 7, 2014

First of all, thank you for this gist! It's really helpful. However it's not working for me, and I'm not sure where the problem is. I copied your gist and adjusted the paths. This is the exception I am getting:

logstash@asgard:~$ service logstash-reader status
/etc/init.d/logstash-reader: invalid arguments
 * could not access PID file for logstash-reader
logstash@asgard:~$ service logstash-reader start
/etc/init.d/logstash-reader: invalid arguments
 * could not access PID file for logstash-reader
 * Starting reader logstash-reader                                                              
/etc/init.d/logstash-reader: 28: /etc/init.d/logstash-reader: start-stop-daemon: not found
                                                                                         [fail]

Do you have any ideas what could be the problem?

@blisteringherb

This comment has been minimized.

Copy link

commented Aug 20, 2014

If you edited the logstash_bin= line, you may need to make sure that the command is preceded by --.

logstash_bin="-- /opt/logstash/logstash-1.4.2/bin/logstash"

@sumitgupta0001

This comment has been minimized.

Copy link

commented Mar 28, 2016

I got the same error:

  • could not access PID file for logstash-shipper
  • Starting shipper logstash-shipper

i tried with above soln but still no luck ...plss help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.