# Use Env variable causing Elasticsearch not to start

**URL:** <https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053>\
**Category:** Elasticsearch\
**Created:** [February 5, 2019, 4:34am UTC](https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053 "2019-02-05T04:34:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hari\_Prasad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hari_prasad/32/56784_2.png) [@Hari\_Prasad](https://discuss.elastic.co/u/Hari_Prasad)\
**Post date:** [February 5, 2019, 4:34am UTC](https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053/1 "2019-02-05T04:34:18Z")

</div>

I have added an env variable in elasticsearch.yml file as follows

`http.port: ${PORT}`

I am able to retrieve the value of this variable when I echo the value of the variable

```
echo $PORT
9200

```

But the Elasticsearch do not start with this configuration. Infact it do not start at when I use ${} values and only does when i comment it out or substitute it with actual values.

Please help me with any configuration that I am missing.

Thank you

---

<div class="post-metadata">

**Author:** ![Bernt\_Rostad](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Bernt\_Rostad](https://discuss.elastic.co/u/Bernt_Rostad)\
**Post date:** [February 5, 2019, 7:18am UTC](https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053/2 "2019-02-05T07:18:33Z")

</div>

> [@Hari\_Prasad](#):
>
> I have added an env variable in elasticsearch.yml file as follows
> 
> `http.port: ${PORT}`

I'm doing exactly the same and it works for me, but you must remember to export PORT in the environment of the user that starts Elasticsearch which may be a different user from the one you're logged in as.

One way around this problem is to use a wrapper script to set the environment variable and start Elasticsearch. For instance a _start-es.sh_ with this content:

```
#!/bin/bash

export PORT=9255 # http.port
export TCP_PORT=9355 # transport.tcp.port

#now start Elasticsearch as a daemon process:
/basedir/es6.5.4/bin/elasticsearch -d -p /basedir/pids/es.pid 2>/basedir/logs/es.stderr

```

Then you just run _./start.es.sh_ when you want to start Elasticsearch with the right PORT setting.

Of course, you can then expand the script to add even more environment variables, for instance node and cluster name, zen discovery hosts, data path etc making _elasticsearch.yml_ completely generic across all nodes and even across clusters, making maintenance a breeze.

Good luck!

---

<div class="post-metadata">

**Author:** ![Hari\_Prasad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hari_prasad/32/56784_2.png) [@Hari\_Prasad](https://discuss.elastic.co/u/Hari_Prasad)\
**Post date:** [February 5, 2019, 9:06am UTC](https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053/3 "2019-02-05T09:06:46Z")

</div>

Found the solution for this  
As mentioned the below link

> <https://stackoverflow.com/questions/24964018/elasticsearch-cant-resolve-environment-variables-in-elasticsearch-yml>

As I am running elasticsearch as a linux service, rather than a shell application, it has access to no environment variables except for a very select few.

I added the following line to the end of `/etc/sysconfig/elasticsearch` to load the environment variables I wanted available to the program:

`. /path/to/environment/variables`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 5, 2019, 9:06am UTC](https://discuss.elastic.co/t/use-env-variable-causing-elasticsearch-not-to-start/167053/4 "2019-03-05T09:06:47Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
