1

I have a command that takes a pid and operates on it.

Works great.

lsof -p 1112| wc -l

But when I use the approach to pipe in the pid, that I normally use, it fails because this is a java app:

lsof -p (ps -e | grep logstash) | wc -l

it fails to work, because java apps do not show up by their name in ps -e, rather they show up as java. (Which doesn't help, because there are multiple java apps)

You can see logstash 7 up from the bottom of this output from ps aux

498       1795 16.9 50.7 551391388 12422888 ?  Sl   Dec14 1425:36 /usr/bin/java
root      1896  0.0  0.0  80900  3344 ?        Ss   Dec14   0:01 /usr/libexec/po
postfix   1901  0.0  0.0  81152  3360 ?        S    Dec14   0:00 qmgr -l -t fifo
root      1926  0.0  0.0 183032  1792 ?        Ss   Dec14   0:00 /usr/sbin/abrtd
root      1938  0.0  0.0 116880  1260 ?        Ss   Dec14   0:00 crond
root      1957  0.0  0.0  21108   492 ?        Ss   Dec14   0:00 /usr/sbin/atd
root      1992  0.0  0.0   4064   512 tty1     Ss+  Dec14   0:00 /sbin/mingetty
root      1994  0.0  0.0   4064   516 tty2     Ss+  Dec14   0:00 /sbin/mingetty
root      1996  0.0  0.0   4064   512 tty3     Ss+  Dec14   0:00 /sbin/mingetty
root      1998  0.0  0.0   4064   516 tty4     Ss+  Dec14   0:00 /sbin/mingetty
root      2000  0.0  0.0   4064   516 tty5     Ss+  Dec14   0:00 /sbin/mingetty
root      2002  0.0  0.0   4064   512 tty6     Ss+  Dec14   0:00 /sbin/mingetty
logstash 37916 10.7  2.2 4767300 553372 ?      SNsl Dec19 167:39 /usr/bin/java -
root     37972  0.0  0.0      0     0 ?        S    Dec19   1:12 [flush-253:2]
postfix  47810  0.0  0.0  80980  3384 ?        S    13:30   0:00 pickup -l -t fi
root     48006  0.0  0.0      0     0 ?        S    14:00   0:00 [flush-253:3]
root     48064  0.1  0.0 104616  4592 ?        Ss   14:04   0:00 sshd: root@pts/
root     48066  0.0  0.0 108352  1828 pts/0    Ss   14:04   0:00 -bash
root     48083  0.0  0.0 110240  1136 pts/0    R+   14:05   0:00 ps aux

What is the way to grep out the pid for logstash ?

  • How do you know that 1112 is the right pid? – Jeff Schaller Nov 29 '17 at 22:05
1

You can hack around the problem, by passing a random option to the process when starting it like this:

$ ls
Hello.class
$ java -Dprocess_name=Logstash_or_whatever Hello
$ ps -e | grep process_name=Logstash_or_whatever

What do you think? Ugly but workable?

  • Interesting, but a non starter in the real world of production machines. – samsmith Nov 29 '17 at 21:56
  • Well, I live in the same world of braindead production java applications that feel like their creators have yet to see their first live Unix shell. That said, what you can do is talk to your production/applications people to get a clue. The other thing you can do is to try to find a regex that matches what you can see in ps -e. And yet another thing is to find the file that contains the PID since at most Java applications are at least as sane to have a start script that saves their PID somewhere in a file... – Tomáš Pospíšek Nov 29 '17 at 22:03
0

We established two solutions:

var1=`pgrep -f logstash`; ls -al /proc/$var1/fd |wc -l

or, more hack,

ls -al /proc/`pgrep -f logstash`/fd |wc -l

Note the use of back ticks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.