ElasticSearch start, stop, and restart

I use ElasticSearch, and I love it.
ElasticSearch bonsai tree
However, on my laptop, I frequently close and open my lid, thus suspending my system, and ElasticSearch doesn’t usually recover from that. So, I end up needing to kill ElasticSearch, and then starting it again. My old way was to hunt and peck to find the process, kill that, wait a moment, and then type an incredibly long command to start it again.

ps aux | grep ElasticSearch

peterdietz 25760 0.0 19.9 5969416 3330804 s002 S Sat07PM 13:12.16 /usr/bin/java -Xms256m -Xmx3g -Xss256k -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Delasticsearch -Des.path.home=/Volumes/Data/elastic-testing/elasticsearch-0.19.11 -cp :/Volumes/Data/elastic-testing/elasticsearch-0.19.11/lib/elasticsearch-0.19.11.jar:/Volumes/Data/elastic-testing/elasticsearch-0.19.11/lib/*:/Volumes/Data/elastic-testing/elasticsearch-0.19.11/lib/sigar/* org.elasticsearch.bootstrap.ElasticSearch

kill -9 25760


/Volumes/Data/elastic-testing/elasticsearch-0.19.11/bin/elasticsearch

Thats no fun. So here’s the changes I made to find a better way. First, install the ElasticSearch Java Service Wrapper.

cd ~/Downloads
git clone https://github.com/elasticsearch/elasticsearch-servicewrapper.git
cd elasticsearch-servicewrapper
sudo ln -s /Volumes/Data/elastic-testing/elasticsearch-0.19.11 /usr/local/share/elasticsearch
sudo mv service /usr/local/share/elasticsearch/bin/

Then, at this point the ElasticSearch service wrapper is ready to go, you can test it by directly invoking the service:

/usr/local/share/elasticsearch/bin/service/elasticsearch

Usage: /usr/local/share/elasticsearch/bin/service/elasticsearch [ console | start | stop | restart | condrestart | status | install | remove | dump ]

Commands:
  console      Launch in the current console.
  start        Start in the background as a daemon process.
  stop         Stop if running as a daemon or in another console.
  restart      Stop if running and then start.
  condrestart  Restart only if already running.
  status       Query the current status.
  install      Install to start automatically when system boots.
  remove       Uninstall.
  dump         Request a Java thread dump if running.

So, pretty cool, I’ll go one step further, and shrink how much I’ll actually need to type. Edit your bash profile ~/.bash_profile and add the following.

alias es=elasticsearch

elasticsearch() {
  /usr/local/share/elasticsearch/bin/service/elasticsearch $1
}

Then you’ll need to re-source your bash profile.

. ~/.bash_profile

What this gives you is es is now mapped to a function that accepts an argument. The magic happens when you type:

es restart

Stopping ElasticSearch...
Waiting for ElasticSearch to exit...
Waiting for ElasticSearch to exit...
Stopped ElasticSearch.
Starting ElasticSearch...
Waiting for ElasticSearch...
.
.
.
running: PID:31782

So, now that your development process for interacting is that much more pleasant, or quicker, go forth to make more cool things.