0

I'm using AWS EC2 and a custom script that needs to run:

java -jar logstash-1.3.3-flatjar.jar agent -f shipper.conf

But it only seems to work after I ssh into the server and run it manually. I found this link: "Linux Launch java program on startup (EC2 instance)"

But that did not help me. How can I get my bash script to run this command? I even tried creating a second shell script that only executes my logstash java script.

Community
  • 1
  • 1
Gabriel
  • 575
  • 2
  • 8
  • 20
  • Your best bet is to create an init script that can start the task for you and place that init script into your default runlist. How to do this depends on your distribution. You can do it very easily in upstart ( which is awesome because it can do pid management and such ) but that only works in some distros ( ubuntu based, maybe others). Can you provide information as to your results with the method in the link you provided? They seemed to be on the right track. – erik258 Mar 07 '14 at 22:22
  • When I tried that method the instance was not able to boot up. Maybe I didn't do it right. I'm running fedora 17 btw. – Gabriel Mar 09 '14 at 00:04

1 Answers1

0

There is no need to write your own initscript, logstash website provides the code. Copy the script in /etc/init.d/logstash, then run chkconfig --add logstash and chkconfig logstash on to enable it; then you can control it using the service command as usual. Note there is an environment variable configuration section you need to edit to adjust to your system.

#! /bin/sh
#
#   /etc/rc.d/init.d/logstash
#
#   Starts Logstash as a daemon
#
# chkconfig: 2345 20 80
# description: Starts Logstash as a daemon
# pidfile: /var/run/logstash-agent.pid

### BEGIN INIT INFO
# Provides: logstash
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Logstash
# Description: Starts Logstash as a daemon.
# Modified originally from https://gist.github.com/2228905#file_logstash.sh
### END INIT INFO

### CONFIGURATION 
# Amount of memory for Java
#JAVAMEM=256M
# Location of logstash files
LOCATION=/opt/logstash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DESC="Logstash Daemon"
NAME=java
DAEMON=$(which java)
CONFIG_DIR=/opt/logstash/logstash.conf
LOGFILE=/opt/logstash/logstash.log
JARNAME=logstash-monolithic.jar
#ARGS="-Xmx$JAVAMEM -Xms$JAVAMEM -jar ${JARNAME} agent --config ${CONFIG_DIR} --log ${LOGFILE} --grok-patterns-path ${PATTERNSPATH}"
ARGS="-jar ${JARNAME} agent --config ${CONFIG_DIR} --log ${LOGFILE}"
SCRIPTNAME=/etc/init.d/logstash
PIDFILE=/var/run/logstash.pid
base=logstash
### END CONFIGURATION    

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
{
  echo "Couldn't find $DAEMON"
  exit 99
}
fi

. /etc/init.d/functions

#
# Function that starts the daemon/service
#
do_start()
{
  cd $LOCATION && \
  ($DAEMON $ARGS &) \
  && success || failure
}

set_pidfile()
{
  pgrep -f "$DAEMON[[:space:]]*$ARGS" > $PIDFILE
}

#
# Function that stops the daemon/service
#
do_stop()
{
  pid=`cat $PIDFILE`
  if checkpid $pid 2>&1; then
       # TERM first, then KILL if not dead
       kill -TERM $pid >/dev/null 2>&1
       usleep 100000
       if checkpid $pid && sleep 1 &&
                 checkpid $pid && sleep $delay &&
                 checkpid $pid ; then
            kill -KILL $pid >/dev/null 2>&1
            usleep 100000
       fi
   fi
   checkpid $pid
   RC=$?
   [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"

}

case "$1" in
  start)
    echo -n "Starting $DESC: "
    do_start
    touch /var/lock/subsys/$JARNAME
    set_pidfile
    ;;
  stop)
    echo -n "Stopping $DESC: "
    do_stop
    rm /var/lock/subsys/$JARNAME
    rm $PIDFILE
    ;;
  restart|reload)
    echo -n ""Restarting $DESC: "
    do_stop
    do_start
    touch /var/lock/subsys/$JARNAME
    set_pidfile
    ;;
  status)
    status -p $PIDFILE
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
    exit 3
    ;;
esac

echo
exit 0
guido
  • 18,864
  • 6
  • 70
  • 95
  • Thanks! There's a typo in this script though: restart|reload) echo -n ""Restarting $DESC: " should only be: restart|reload) echo -n "Restarting $DESC: " – Gabriel Mar 10 '14 at 15:01
  • I had to add sincedb_path to my script. But now when I run service logstash start the prompt just hangs. It's running though but I'm not sure why it's doing this. – Gabriel Mar 10 '14 at 15:35
  • and /var/log/messages is getting populated with: Mar 10 13:00:07 localhost logstash[592]: \\\\\\\\\\\\\\. No idea whats going on there. – Gabriel Mar 10 '14 at 17:15