Run Installed Elasticsearch from Your Shell

Doug TurnbullApril 21, 2016

Got Elasticsearch installed on an Ubuntu/Debian box? Want to run it directly from the command line? It’s handy to recreate how Elasticsearch actually runs on your box. You might want to debug something. Or perhaps you’d just like to see the log statements scroll by this console window. This might also be handy if you’re running the zip or tarred versions directly and want to know what all the knobs and dials do.

I’ve pieced this together by

  1. Observing how the service script /etc/init.d/elasticsearch runs the Elasticsearch bootstrap script. It sets up a variety of environment variables (detailed below) that configure Elasticsearch.
  2. Observing how the bootstrip script /usr/share/elasticsearch/bin/elasticsearch runs Elasticsearch’s Java code. It sets up JVM settings and executes Elasticsearch’s Java main class.

Home sweet ES_HOME

First things first. The most important thing when setting up Elasticsearch is to set your ES_HOME. ES_HOME is an environment variable pointing where Elasticsearch is installed. If no other settings are specified, Elasticsearch operates solely out of this directory. Open up a shell and set this all important variable:

export ES_HOME=/usr/share/elasticsearch

Other destinations

But ES_HOME doesn’t have the final say. Elasticsearch will use other directories for logs, data, and configuration. Indeed, in my debian package install of Elasticsearch, I have files flung all over /var and /etc. Let’s set those environment variables as well.

export LOG_DIR=/var/log/elasticsearch
export DATA_DIR=/var/lib/elasticsearch
export CONF_DIR=/etc/elasticsearch

Running Elasticsearch

Finally, startup Elasticsearch as the elasticsearch user, passing in arguments that Elasticsearch’s bootstrap script expects. This form of running this script is plucked out of the service script. But I’ve made a few alterations. I remove the -d argument so Elasticsearch isn’t daemonized. I’ve also added bash -x to observe the bash commands as they’re running.

sudo -u elasticsearch bash -x $ES_HOME/bin/elasticsearch --default.path.home=$ES_HOME --default.path.logs=$LOG_DIR --default.path.data=$DATA_DIR --default.path.conf=$CONF_DIR

You’ll have something like the following:

alt text

Now verify Elasticsearch is operational with curl

doug@76$~ $ curl -XGET http://localhost:9200
{
  "name" : "Alexander Lexington",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.2.2",
    "build_hash" : "fcc01dd81f4de6b2852888450ce5a56436fd5852",
    "build_timestamp" : "2016-03-29T08:49:35Z",
    "build_snapshot" : false,
    "lucene_version" : "5.4.1"
  },
  "tagline" : "You Know, for Search"
}

Running Elasticsearch with Java directly

If you want to be extra clever, observe the Java command output by the bin/elastcsearch bootstrap script. Fetch it. Tinker with it! You can run Elasticsearch directly with Java, just by adding sudo -u elasticsearch to the command:

sudo -u elasticsearch /usr/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/usr/share/elasticsearch -cp '/usr/share/elasticsearch/lib/elasticsearch-2.2.2.jar:/usr/share/elasticsearch/lib/*' org.elasticsearch.bootstrap.Elasticsearch start --default.path.home=/usr/share/elasticsearch --default.path.logs=/var/log/elasticsearch --default.path.data=/var/lib/elasticsearch --default.path.conf=/etc/elasticsearch```

From here you can get into real trouble! You could add commandline parameters for remote debugging. Blow Elasticsearch up with alternate garbage collection settings. Whatever mayhem happens to be required at the moment.

I should add that inspecting ./bin/elasticsearch, there’s a better way to pass, say, remote debugging arguments. In reality, what you should do is use ES_JAVA_OPTS to pass aditional JVM params like so:

export ES_JAVA_OPTS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=n"

And be sure to pass this to the shell your sudo command triggers:

 sudo -u elasticsearch ES_JAVA_OPTS=$ES_JAVA_OPTS bash -x $ES_HOME/bin/elasticsearch --default.path.home=$ES_HOME --default.path.logs=$LOG_DIR --default.path.data=$DATA_DIR --default.path.conf=$CONF_DIR

Logging – want to make your eyes bleed?

Now its not very interesting to run Elasticsearch from the commandline if there’s nothing to look at. So how can we turn on some console logging?

One option is to edite /etc/elasticsearch/logging.yml to change logging levels, pushing stuff out to the console, etc.

But its far simpler just to use ES_JAVA_OPTS from the previous section to override the logging level temporarily. The console appender is already enabled for the root logger, so simply set your desired logging level temporarily:

export ES_JAVA_OPTS="-Des.logger.level=INFO"

and reexecute.

 sudo -u elasticsearch ES_JAVA_OPTS=$ES_JAVA_OPTS bash -x $ES_HOME/bin/elasticsearch --default.path.home=$ES_HOME --default.path.logs=$LOG_DIR --default.path.data=$DATA_DIR --default.path.conf=$CONF_DIR

Roll that beautiful Elasticsearch bean footage:

alt text

That’s it! And as always if you need Elasticsearch consulting help, don’t hesitate to get in touch!.




More blog articles:


Leverage our Elasticsearch expertise!

We've written books and built courses on Elasticsearch. We've helped clients like Stackify and the Education Advisory Board. We've spoken at Elastic{On} on Elasticsearch relevance.

Learn More about our Elasticsearch services.