# How do I use environment variables when running as service?

**URL:** <https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392>\
**Category:** Logstash\
**Created:** [January 12, 2017, 2:45pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392 "2017-01-12T14:45:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)\
**Post date:** [January 12, 2017, 2:45pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/1 "2017-01-12T14:45:37Z")

</div>

I am using Logstash v5.1.0 on Ubuntu 16.04 and my logstash conf file includes an environment variable reference to set the output graphite server, but I cannot get it to work when running as a service.

My config file contains (missing a few lines for clarity):  
graphite {  
host =\> "${GRAPHITE\_SERVER}"  
}

and my /etc/environment file contains:  
GRAPHITE\_SERVER=54.40.235.208

Yet with configuration file logging, I see:  
fetched an invalid config {}  
reason=\>"Cannot evaluate `${GRAPHITE_SERVER}`. Environment variable `GRAPHITE_SERVER` is not set and there is no default value given."

This does work if I run logstash from bash manually.

Any help would be much appreciated - thanks.  
Andrew

---

<div class="post-metadata">

**Author:** ![jakommo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakommo/32/6716_2.png) [@jakommo](https://discuss.elastic.co/u/jakommo)\
**Post date:** [January 12, 2017, 4:00pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/2 "2017-01-12T16:00:20Z")

</div>

Hi @astickler,

I haven't tried it on 16.04 but on 14.04 it works when exporting it in `/etc/default/logstash`.  
E.g. `export GRAPHITE_SERVER=54.40.235.208`.  
I know, it sounds strange to prefix with `export` in that file, see [https://github.com/elastic/logstash/issues/6414](https://github.com/elastic/logstash/issues/6414) for discussion.

14.04 uses upstart while 16.04 is using systemd, so they are probably souring the file in different ways, but it's worth a try.

---

<div class="post-metadata">

**Author:** ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)\
**Post date:** [January 13, 2017, 11:17am UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/3 "2017-01-13T11:17:14Z")

</div>

Hi Jakob - I had already tried that, but it also doesn't work.  
Andrew

---

<div class="post-metadata">

**Author:** ![jakommo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakommo/32/6716_2.png) [@jakommo](https://discuss.elastic.co/u/jakommo)\
**Post date:** [January 13, 2017, 3:37pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/4 "2017-01-13T15:37:13Z")

</div>

I just tried this on a 16.04 VM and adding it to `/etc/default/logstash` with the `export` prefixed resulted in:

```
systemd[1]: logstash.service: Ignoring invalid environment assignment 'export LS_FPATH=/tmp/': /etc/default/logstash

```

However, just using `LS_FPATH="/tmp/"` worked for me.

```
# cat /etc/default/logstash 
LS_FPATH="/tmp/"

# echo 123 | ncat localhost 5011

# cat /tmp/lsout.log 
{"@timestamp":"2017-01-13T15:34:23.971Z","port":43886,"@version":"1","host":"127.0.0.1","message":"123"}

# cat /etc/logstash/conf.d/ls.conf 
#
input {
	tcp { port => 5011}
}

output {
	file {
		path => "${LS_FPATH}/lsout.log"
	}
}

```

I tested with Logstash 5.1.2, mind giving that a try and using my simple config, for testing?

---

<div class="post-metadata">

**Author:** ![astickler](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@astickler](https://discuss.elastic.co/u/astickler)\
**Post date:** [January 13, 2017, 4:59pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/5 "2017-01-13T16:59:43Z")

</div>

Jakob - this is good news, your test worked with my currently installed 5.1.1 version, _and_ when I tried the same with my config.

I know I tried this before and it didn't work, but I have realised I may have been confused by old errors in the logstash\_plain log because my service is taking nearly 2 minutes to start (I am on an AWS instance, and although I have improved the entropy, it appears it's not yet good enough).

Anyway - all working now - thanks for your help.  
Andrew

---

<div class="post-metadata">

**Author:** ![jakommo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakommo/32/6716_2.png) [@jakommo](https://discuss.elastic.co/u/jakommo)\
**Post date:** [January 13, 2017, 5:14pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/6 "2017-01-13T17:14:57Z")

</div>

Glad to hear it's working now. About the entropy issue, see this [GH issue](https://github.com/elastic/logstash/issues/6117#issuecomment-270915288), you might want to give `haveged` a try. Worked good for me to resolve this.

---

<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:** [February 10, 2017, 5:15pm UTC](https://discuss.elastic.co/t/how-do-i-use-environment-variables-when-running-as-service/71392/7 "2017-02-10T17:15:11Z")

</div>

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