# Elastic APM- apm-agent-attach-cli.jar: Stop collecting metrics through apm cli

**URL:** <https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592>\
**Category:** APM\
**Tags:** java\
**Created:** [October 23, 2023, 7:23pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592 "2023-10-23T19:23:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vamshigottam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vamshigottam/32/126778_2.png) [@vamshigottam](https://discuss.elastic.co/u/vamshigottam)\
**Post date:** [October 23, 2023, 7:23pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/1 "2023-10-23T19:23:18Z")

</div>

We are capturing the application metrics with elastic APM agent using apm-agent-attach-cli.jar which attaches to the running JVM's without making any changes to the code or JVM startup.  
This is working fine without any issues and metrics are visible on UI.  
however we cannot detach apm-agent-attach-cli.jar from running JVM unless we restart the JVM Process.  
Could you share how this can be tackled through apm-agent-attach-cli.jar.

Documentation followed: [Automatic setup with apm-agent-attach-cli.jar | APM Java Agent Reference [1.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/setup-attach-cli.html)

command used to attach running JVM

```auto
java -jar apm-agent-attach-cli.jar \
    --exclude-user root \
    --include-vmarg XXXXXXXXXX \
    --continuous \
    --config application_packages=XXX \
    --config server_url=http://localhost:8282 \
    --agent-jar elastic-apm-agent-1.43.0.jar &

```

**Kibana version** : 8.8.2

**Elasticsearch version** : 8.8.2

**APM Server version** : 8.8.2

**APM Agent language and version** : Java

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [October 24, 2023, 10:14am UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/2 "2023-10-24T10:14:06Z")

</div>

You can't "detach" the apm-agent from a process. What you can do is the following:

- When attaching, configure the agent to fetch its [configuration from a file](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-config-file)
- Configure [recording](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-recording) to `true` / `false` in that file. The agent will periodically reload the file and disable/enable itself accordingly.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Rudra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeep_rudra/32/126805_2.png) [@Sandeep\_Rudra](https://discuss.elastic.co/u/Sandeep_Rudra)\
**Post date:** [October 24, 2023, 6:03pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/3 "2023-10-24T18:03:11Z")

</div>

Hi Jonas, Getting the configurations from properties file, APM agent is still pushing metrics to APM server. Is there anything else we need to look at to stop the metrics from being sent to APM server? We also tried disable\_send to true and Instrument to false

Properties file:

```auto
elastic.apm.server_url=https://dc1-elsticuat08.ulinedm.com:8282
elastic.apm.recording=false
elastic.apm.application_packages=com.uline

```

Script we are running:

```auto
java -jar apm-agent-attach-cli.jar \
    --exclude-user root \
    --include-vmarg consul.port=8500 \
    --continuous \
    --config config_file=true
    --agent-jar elastic-apm-agent.jar

```

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [October 25, 2023, 11:47am UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/4 "2023-10-25T11:47:58Z")

</div>

Two things I can spot here:

- `config_file` is not a boolean option, but a string. It should point to the file location.
- the configuration options in the properties file must be given without the `elastic.apm.` prefix.

I suggest you fix those two issues and then check the APM Agent debug logs to see whether the options are correctly picked up.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Rudra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeep_rudra/32/126805_2.png) [@Sandeep\_Rudra](https://discuss.elastic.co/u/Sandeep_Rudra)\
**Post date:** [October 25, 2023, 2:08pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/5 "2023-10-25T14:08:14Z")

</div>

Thank you Jonas for the reply. I made the changes you suggested, below you should see the poperties and script we used. Still getting trace metrics to APM server.

**Output after the script ran:**

The tracer said paused but we are still seeing the transactions coming into Kibana APM. Since the agent is already attached it is throwing an Error. but eventually when I left the process running for 1 hour it eventually said tracer paused. Highled in the screenshot below.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/2/12355cdcbdd66697d98419596c89a8972cd06472.png)

**Script:**

```auto
java -jar apm-agent-attach-cli.jar \
    --exclude-user root \
    --include-vmarg consul.port=xxxx\
    --continuous \
    --config config_file="/apm-agent-attach/elasticapm.properties" \
    --agent-jar elastic-apm-agent-1.43.0.jar &

```

**Properties:**

```auto
server_url=https://xxxxx.com:8200
recording=false
application_packages=com.example.org

```

Please let me know if you need any more details.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Rudra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeep_rudra/32/126805_2.png) [@Sandeep\_Rudra](https://discuss.elastic.co/u/Sandeep_Rudra)\
**Post date:** [October 25, 2023, 8:11pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/6 "2023-10-25T20:11:55Z")

</div>

![KibanaSS](https://us1.discourse-cdn.com/elastic/original/3X/7/7/77033c4ff39c9e6a3f00b395a2953da054b910d1.png)

Still getting transactions to Kibana from APM Agent.

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [October 26, 2023, 11:05am UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/7 "2023-10-26T11:05:03Z")

</div>

If I understand you correctly, you are running the attacher again, in order to make the agent "stop". This won't work and rather cause harm, because you'll have two agents running in parallel on the same process.

You instead should do the following:

- When attaching the agent for the first time to the process, provide the `--config config_file="/apm-agent-attach/elasticapm.properties"` option
  - The properties file should contain `recording=true`

- When you now want to disable the agent, simply edit the config file to `recording=false`. The agent will reload the configuration after at most 30 seconds and disable itself. You can reenable it by editing the config file back to `recording = true`

---

<div class="post-metadata">

**Author:** ![Sandeep\_Rudra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeep_rudra/32/126805_2.png) [@Sandeep\_Rudra](https://discuss.elastic.co/u/Sandeep_Rudra)\
**Post date:** [October 26, 2023, 8:37pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/8 "2023-10-26T20:37:44Z")

</div>

Thank you Jonas for quick responses:

When we tried on a new server it started working. The servers where the agent was already running, it did not work. Maybe we need to restart the applications to take effect.

Question, When I run "java -jar ...." The agent is starting but after a while I have to do ctl+c to kill the process. Since it keeps going. Is this normal? If we want to automate the installation process using Ansible, should we install it in a different way?

Also one more, even after we kill the apm process the metrics are still coming in. How does the agent still keep working after killing the apm process?

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [October 27, 2023, 1:41pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/9 "2023-10-27T13:41:50Z")

</div>

> When we tried on a new server it started working. The servers where the agent was already running, it did not work. Maybe we need to restart the applications to take effect.

As I already said, you must not run the agent twice on the same process!

> Question, When I run "java -jar ...." The agent is starting but after a while I have to do ctl+c to kill the process. Since it keeps going. Is this normal? If we want to automate the installation process using Ansible, should we install it in a different way?

Do you mean you start your application via "java -jar ...." or the agent attacher?

> Also one more, even after we kill the apm process the metrics are still coming in. How does the agent still keep working after killing the apm process?

There is no separate "apm-process". The attacher forces your application-JVM to load and start the apm agent. The attacher process itself doesn't send any data.

---

<div class="post-metadata">

**Author:** ![Sandeep\_Rudra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sandeep_rudra/32/126805_2.png) [@Sandeep\_Rudra](https://discuss.elastic.co/u/Sandeep_Rudra)\
**Post date:** [October 30, 2023, 3:01pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/10 "2023-10-30T15:01:01Z")

</div>

Thank you for reply Jonas. I got all the answers I need thank you for being so responsive and helpful. 🙏

---

<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:** [November 27, 2023, 3:01pm UTC](https://discuss.elastic.co/t/elastic-apm-apm-agent-attach-cli-jar-stop-collecting-metrics-through-apm-cli/345592/11 "2023-11-27T15:01:22Z")

</div>

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