# \[SOLVED\]Count the number of running process with Telegraf

**URL:** https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972
**Category:** Telegraf
**Tags:** telegraf
**Created:** [February 12, 2018, 9:15am UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972 "2018-02-12T09:15:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Bugsyaya](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bugsyaya/32/1009_2.png) [@Bugsyaya](https://community.influxdata.com/u/Bugsyaya)
#### Post date: [February 12, 2018, 9:15am UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972/1 "2018-02-12T09:15:17Z")

</div>

Hey,

I’m using telegraf, influxdb and grafana to make a monitoring system for a distributed application. The first thing I want to do is to count the number of java process running on a machine.

But when I make my request, the result are egal to the correct process number or the correct process number + 1.

I’m running centos 7 and Telegraf v1.5.0 (git: release-1.5 a1668bbf)

All Java process I want to count :

> ```
> [root@localhost ~]# pgrep -f java
> 10665
> 10688
> 10725
> 10730
> 11104
> 11174
> 16298
> 22138
> 
> ```

My telegraf.conf :

> [global\_tags]
> 
> # Configuration for telegraf agent
> 
> [agent]  
> interval = “5s”  
> round\_interval = true  
> metric\_batch\_size = 1000  
> metric\_buffer\_limit = 10000  
> collection\_jitter = “0s”  
> flush\_interval = “10s”  
> flush\_jitter = “0s”  
> precision = “”  
> debug = true  
> quiet = false  
> logfile = “/var/log/telegraf/telegraf.log”  
> hostname = “my\_server”  
> omit\_hostname = false

My input.conf :

> # Read metrics about disk usagee
> 
> [[inputs.disk]]  
> fielddrop = [“inodes\*”]  
> mount\_points=[“/”, “/workspace”]
> 
> # File
> 
> [[inputs.filestat]]  
> files = [“myfile.log”]  
> [[inputs.exec]]
> 
> ## Commands array
> 
> commands = [“/etc/telegraf/telegraf.d/customScript.sh”]  
> name\_override = “java\_process”  
> data\_format = “value”

My /etc/telegraf/telegraf.d/customScript.sh :

> #!/bin/bash  
> ps -aux | grep java | wc -l

I think the problem comes to `grep` but I don’t know how resolve this. I tried to use `pgrep -f -c java` but the result are same.

---

<div class="post-metadata">

### Author: ![noahcrowley](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/noahcrowley/32/818_2.png) [@noahcrowley](https://community.influxdata.com/u/noahcrowley)
#### Post date: [February 12, 2018, 2:57pm UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972/2 "2018-02-12T14:57:23Z")

</div>

When using `grep` with the output of `ps`, one of the matches will be the `grep` process itself.

For example, I am running two python processes on my machine. Here’s what happens when I pipe `ps` to `grep`:

```bash
$ ps -aux | grep python
vagrant 1480 0.0 0.6 31348 6408 pts/1 S+ 14:39 0:00 python
vagrant 1512 0.0 0.6 31348 6416 pts/3 S+ 14:40 0:00 python
vagrant 1515 0.0 0.0 14224 932 pts/2 S+ 14:40 0:00 grep --color=auto python

```

There are three lines: the two python processes, and the `grep` process and its arguments. Because of this, you can’t count the number of lines to determine the number of processes.

Using `pgrep` with the `-c` argument should work. If I test with my machine running two python processes this is the result:

```bash
$ pgrep -c python
2

```

---

<div class="post-metadata">

### Author: ![Bugsyaya](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bugsyaya/32/1009_2.png) [@Bugsyaya](https://community.influxdata.com/u/Bugsyaya)
#### Post date: [February 12, 2018, 3:05pm UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972/3 "2018-02-12T15:05:00Z")

</div>

Yes, I see what did you explain. But I have tried to use `pgrep -f -c java` and the result are the same (some time 8, some time 9). Whether I use `ps -aux | grep java | wc -l` or `pgrep -f -c java`, the result are the same. However, in the first case, the result should always be 9 and in the second case always 8. Except it’s not the case and i don’t understand why.

---

<div class="post-metadata">

### Author: ![noahcrowley](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/noahcrowley/32/818_2.png) [@noahcrowley](https://community.influxdata.com/u/noahcrowley)
#### Post date: [February 12, 2018, 3:20pm UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972/4 "2018-02-12T15:20:03Z")

</div>

The `-f` argument in `pgrep -f -c` matches the full command line. It’s possible that you are matching something other than a java process.

Can you try using `pgrep -c` instead?

---

<div class="post-metadata">

### Author: ![Bugsyaya](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bugsyaya/32/1009_2.png) [@Bugsyaya](https://community.influxdata.com/u/Bugsyaya)
#### Post date: [February 12, 2018, 3:30pm UTC](https://community.influxdata.com/t/solved-count-the-number-of-running-process-with-telegraf/3972/5 "2018-02-12T15:30:03Z")

</div>

Sorry, I didn’t see you didn’t put the -f argument. Looks like it works! Thank you very much !
