Buscar este blog

sábado, 24 de junio de 2017

CentOS - Run Elasticsearch as service

I assume there is a configured Elasticsearch installation in /opt/elasticsearch-1.5.2. The purpose of this post is to show how to configure this tool to run as an auto start service in CentOS.

First of all, is a best practice to use symbolic links with these kind of installations. If you change the program version you only will need to update the symbolic reference.
ln -s /opt/elasticsearch-1.5.2 /opt/elasticsearch

Also another best practice is to run the program with its own user.
useradd elasticsearch

chown -Rf elasticsearch:elasticsearch /opt/elasticsearch
chown -Rf elasticsearch:elasticsearch /opt/elasticsearch-1.5.2

Then, you go to /etc/init.d and create the service file ("inspired" from here and from one my previous post here):
#!/bin/sh
#
# 
# elasticsearch
#
# chkconfig: 2345 90 60

 
 
# Source function library.
. /etc/init.d/functions
 

 
PROGRAM_CONSOLE_LOG=/var/log/elasticsearch/elasticsearch.log
PROGRAM_PIDFILE=/var/run/elasticsearch.pid
PROGRAM_SCRIPT="/opt/elasticsearch/bin/elasticsearch -p $PROGRAM_PIDFILE"


 
 
PROGRAM_USER=elasticsearch
prog='Elasticsearch'
 
 
 
# Set defaults.
export PROGRAM_PIDFILE
 
 
if [ -z "$STARTUP_WAIT" ]; then
  STARTUP_WAIT=30
fi

 
if [ -z "$SHUTDOWN_WAIT" ]; then
  SHUTDOWN_WAIT=30
fi
 
 
 
start() {
  echo -n "Starting $prog: "
  if [ -f $PROGRAM_PIDFILE ]; then
    read ppid < $PROGRAM_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo -n "$prog is already running"
      failure
      echo
      return 1
    else
      rm -f $PROGRAM_PIDFILE
    fi
  fi
  mkdir -p $(dirname $PROGRAM_CONSOLE_LOG)
  cat /dev/null > $PROGRAM_CONSOLE_LOG
  chown -Rf $PROGRAM_USER $(dirname $PROGRAM_CONSOLE_LOG) || true
 
 
  mkdir -p $(dirname $PROGRAM_PIDFILE)
  chown $PROGRAM_USER $(dirname $PROGRAM_PIDFILE) || true
 
   
  daemon --user $PROGRAM_USER --pidfile $PROGRAM_PIDFILE "$PROGRAM_SCRIPT" 1>"$PROGRAM_CONSOLE_LOG" 2>&1 &
  sleep 2
 # pidofproc node > $PROGRAM_PIDFILE
 
  count=0
  launched=false
 
  until [ $count -gt $STARTUP_WAIT ]
  do
    echo 'checking start...'
    grep 'started' $PROGRAM_CONSOLE_LOG > /dev/null
    if [ $? -eq 0 ] ; then
      launched=true
      break
    fi
    sleep 1
    let count=$count+1;
  done
 
  if [ "$launched" = true ] ; then
    echo 'Unable to start program'
  fi 

  success
  echo
  return 0
}
 
stop() {
  echo -n $"Stopping $prog: "
  count=0;
 
  if [ -f $PROGRAM_PIDFILE ]; then
    read kpid < $PROGRAM_PIDFILE
    let kwait=$SHUTDOWN_WAIT
 
    # Try issuing SIGTERM
 
    kill -15 $kpid
    until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
    do
      sleep 1
      let count=$count+1;
    done
 
    if [ $count -gt $kwait ]; then
      kill -9 $kpid
    fi
  fi
  rm -f $PROGRAM_PIDFILE
  success
  echo
}
 
status() {
  if [ -f $PROGRAM_PIDFILE ]; then
    read ppid < $PROGRAM_PIDFILE
    if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
      echo "$prog is running (pid $ppid)"
      return 0
    else
      echo "$prog dead but pid file exists"
      return 1
    fi
  fi
  echo "$prog is not running"
  return 3
}
 
case "$1" in
  start)
      start
      ;;
  stop)
      stop
      ;;
  restart)
      $0 stop
      $0 start
      ;;
  status)
      status
      ;;
  *)
      ## If no parameters are given, print which are avaiable.
      echo "Usage: $0 {start|stop|status|restart|reload}"
      exit 1
      ;;
esac

Finally, you have to make this script as runnable and configure the service as autostart.
chmod +x /etc/init.d/elasticsearch

chkconfig --add elasticsearch
chkconfig elasticsearch on

No hay comentarios:

Publicar un comentario