How to stop ElasticSearch background task

You would tell it to shut it down by sending the appropriate signal (SIGTERM) to the process.

Note that normally you wouldn't be doing this manually, rather making use of whatever service management facilities are provided by your OS.

To simplify the process of knowing the process id, you can pass the -p argument to have it create a pid file when starting.

eg

./bin/elasticsearch -p elasticsearch-pid -d

which creates the file elasticsearch-pid, which you can then use when shutting it down, like so:

kill -SIGTERM `cat elasticsearch-pid`

Also see the Stopping Elasticsearch section of the documentation.