init.d script runs correctly but process doesn't live when booted fully up

Posted by thetrompf on Server Fault See other posts from Server Fault or by thetrompf
Published on 2012-04-03T04:45:26Z Indexed on 2012/04/03 5:31 UTC
Read the original article Hit count: 338

Filed under:

I have a problem with an init.d script

#!/bin/bash

ES_HOME="/var/es/current"

PID=$(ps ax | grep elasticsearch | grep $ES_HOME | grep -v grep | awk '{print $1}')

#echo $PID
#exit 0
case "$1" in
    start)
        if [ -z "$PID" ]; then
            echo "Starting Elasticsearch"
            echo "Starting Elasticsearch" >> /var/tmp/elasticsearch
            su -m elasticsearch -c "${ES_HOME}/bin/elasticsearch"
            exit 0;
        else
            echo "Elasticsearch already running"
            echo "Elasticsearch already running" >> /var/tmp/elasticsearch
            exit 0;
        fi
        ;;
    stop)
        if [ -n "$PID" ]; then
            echo "Stopping Elasticsearch"
            kill ${PID}
            echo "Stopped Elasticsearch"
            exit 0;
        else
            echo "Elasticsearch is not running"
            exit 0;
        fi
        ;;
esac

The scripts runs just file, as I can see in /var/tmp/elasticsearch a new line is added after every boot, but if I run:

/etc/init.d/elasticsearch stop

Just after the server is booted, I get "Elasticsearch is not running", ergo somehow the process does not stay alive. My question is why? and what am I doing wrong?

Thanks in advance.

© Server Fault or respective owner

Related posts about init.d