# Help for configuring apm-agent in a java application that is started as a service on systemd

**URL:** <https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111>\
**Category:** APM\
**Tags:** java\
**Created:** [June 17, 2019, 4:04pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111 "2019-06-17T16:04:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmoreira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmoreira/32/46578_2.png) [@lmoreira](https://discuss.elastic.co/u/lmoreira)\
**Post date:** [June 17, 2019, 4:04pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/1 "2019-06-17T16:04:41Z")

</div>

Dear, how are you?

I am using elasticsearch, logastah, kibana and apm-serv in version 6.7.2, in docker containers and apm-agent in version 1.6.1

I need to run the apm-agent in a java application that runs as a service on the systemd, tried several configurations and none of them has gone up.

Can you please tell me some documentation so that I can figure out how to make the apm-people upload as a service.

Below is the configuration of my service on systemd:

# vi /etc/systemd/system/app.service

Unit]  
Description==APP  
After=syslog.target

[Service]  
User=root  
ExecStart=/srv/apps/app/app.jar  
SuccessExitStatus=143

[Install]  
WantedBy=multi-user.target

Thank you!

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [June 17, 2019, 4:54pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/2 "2019-06-17T16:54:55Z")

</div>

Hi,

have you tried something like

```
ExecStart=/usr/bin/java -javaagent=... -jar /srv/apps/app/app.jar

```

Let me know if that helps

Felix

---

<div class="post-metadata">

**Author:** ![lmoreira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmoreira/32/46578_2.png) [@lmoreira](https://discuss.elastic.co/u/lmoreira)\
**Post date:** [June 17, 2019, 7:28pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/3 "2019-06-17T19:28:09Z")

</div>

felixbarny, how are you?

Here's your tip, I set up ExecStart as below but when I upload the service with the systemctl the service is not over and it returns me the error at the bottom log.

when executing the same command via scrip bash the service goes up normally.

ExecStart=/usr/lib/jvm/jdk1.8.0\_121/bin/java -javaagent=/opt/apm/elastic-apm-agent.jar -Delastic.apm.service\_name=APP-PRODUCTION -Delastic.apm.server\_urls=http://192.168.251.108:8200 -jar /srv/apps/app/app.jar

Jun 17 16:03:03 andromeda systemd[1]: Reloading.  
Jun 17 16:03:04 andromeda systemd[1]: Started ACPI event daemon.  
Jun 17 16:03:09 andromeda systemd[1]: Started APP.  
**Jun 17 16:03:09 andromeda java[20937]: Unrecognized option: -javaagent=/opt/apm/elastic-apm-agent.jar**  
**Jun 17 16:03:09 andromeda java[20937]: Error: Could not create the Java Virtual Machine.**  
**Jun 17 16:03:09 andromeda java[20937]: Error: A fatal exception has occurred. Program will exit.**  
**Jun 17 16:03:09 andromeda systemd[1]: app.service: Main process exited, code=exited,** status=1/FAILURE  
Jun 17 16:03:09 andromeda systemd[1]: app.service: Unit entered failed state.  
Jun 17 16:03:09 andromeda systemd[1]: app.service: Failed with result 'exit-code'

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [June 17, 2019, 7:41pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/4 "2019-06-17T19:41:20Z")

</div>

Sorry for the typo, it's `-javaagent:`, not `-javaagent=`. See also [https://www.elastic.co/guide/en/apm/agent/java/current/setup-javaagent.html](https://www.elastic.co/guide/en/apm/agent/java/current/setup-javaagent.html).

---

<div class="post-metadata">

**Author:** ![lmoreira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmoreira/32/46578_2.png) [@lmoreira](https://discuss.elastic.co/u/lmoreira)\
**Post date:** [June 17, 2019, 7:51pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/5 "2019-06-17T19:51:28Z")

</div>

I tried with the -javaagent: and returned the same error, I will check the link that followed.

Thank you!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [June 18, 2019, 2:22am UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/6 "2019-06-18T02:22:17Z")

</div>

Thanks for trying Elastic Java APM Agent.

1st I would suggest getting the command running from the normal command line first then once you get that working then I would try it in the `app.service`

2nd I also noticed your service name `APP-PRODUCTION` has a `-` in it, so you may need to quote that so it does not get misinterpreted by the `ExecStart` command.

so see if you can get something like this running first then try it in the `app.service`

```
$ /usr/lib/jvm/jdk1.8.0_121/bin/java -javaagent:/opt/apm/elastic-apm-agent.jar   
-Delastic.apm.service_name="APP-PRODUCTION" -Delastic.apm.server_urls="http://192.168.251.108:8200" -Delastic.apm.secret_token="mysecrettoken"
-jar /srv/apps/app/app.jar

```

3rd The other way perhaps is to create a start / stop script and call that from app.service something like this.

> <https://stackoverflow.com/questions/15146049/startup-script-with-systemd-in-linux>

---

<div class="post-metadata">

**Author:** ![lmoreira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmoreira/32/46578_2.png) [@lmoreira](https://discuss.elastic.co/u/lmoreira)\
**Post date:** [June 18, 2019, 7:45pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/7 "2019-06-18T19:45:54Z")

</div>

Hi, Stephenb

Thanks to answer, today I carry started apm-agent for a shell script, but emerged the demanda to monitoring the aplication java they initiate as a service in systemd.

And by the way the apm-agent is spectacular, when I use more and I learn more with the possibility of its use

At.

Att.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [June 20, 2019, 4:18am UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/8 "2019-06-20T04:18:26Z")

</div>

So have you now tried....

```
[Unit]
Description==APP
After=syslog.target

[Service]
User=root
ExecStart=/usr/lib/jvm/jdk1.8.0_121/bin/java -javaagent:/opt/apm/elastic-apm-agent.jar   
-Delastic.apm.service_name="APP-PRODUCTION" -Delastic.apm.server_urls="http://192.168.251.108:8200" -Delastic.apm.secret_token="mysecrettoken"
-jar /srv/apps/app/app.jar

SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
```

---

<div class="post-metadata">

**Author:** ![lmoreira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lmoreira/32/46578_2.png) [@lmoreira](https://discuss.elastic.co/u/lmoreira)\
**Post date:** [June 21, 2019, 2:24pm UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/9 "2019-06-21T14:24:36Z")

</div>

Hello Stephen

I got a form that worked, I got the script that I used to start the APP, which has the content:

# vi app.sh

/usr/lib/jvm/jdk1.8.0\_121/bin/java   
-javaagent: /opt/apm/elastic-apm-agent.jar   
-Delastic.apm.service\_name = "APP-PRODUCTION"   
-Delastic.apm.server\_urls = "[http://192.168.251.108:8200](http://192.168.251.108:8200)"  
-Delastic.apm.secret\_token = "mysecrettoken"   
--jar app.jar

I will configure service to use it.  
ExecStart = / opt / app / app.sh

That way I was able to start the agent, along with the application, but I thought it was a nasty solution, I'll test your suggestion and return.

---

<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:** [July 12, 2019, 10:24am UTC](https://discuss.elastic.co/t/help-for-configuring-apm-agent-in-a-java-application-that-is-started-as-a-service-on-systemd/186111/10 "2019-07-12T10:24:45Z")

</div>

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