I am trying to find all running java process in a unix box. I am confused between
ps –ef|grep java
and
ps –ef|grep *.java
Which one is correct and what is the exact difference?
PS: Both returns different set of results
Output of ps –ef|grep *.java
ps -ef | grep *.java
aaassa1 7507 2304 0 11:49 pts/4 00:00:00 grep *.java
grep
does just text match, it doesn't have idea of process name or filename etc.
that is, if you have a python script called likejava.py
, it will be in result too.
Back to your question, your *.java
is not good, because grep works with regex, not glob patterns.
You could try:
ps -ef|grep $(which java)
this will list java processes, but only for your default java installation. If you have more than one Java installed, e.g. your Jboss with java7, tomcat with java6, and eclipse with java5, this will fail.
There is another tool called pgrep
. You could give it a try, e.g.
pgrep -l java
you could do man pgrep
to get more info:
pgrep, pkill - look up or signal processes based on name and other attributes
EDIT add a small ps..|grep trick
just saw your output in the question:
You have grep *.java
in output, it is a good example. grep *.java
is not java process but it has text "java". so it is there.
You could avoid it by ps...|grep [j]ava
works for bash.
The first is correct. In the second, your shell expands the *.java
based on your current working directory, with fun results.
Exactly what those results are... I'm guessing here, but assuming you have files foo.java
and bar.java
in your current dir, the latter is effectively doing:
ps -ef | grep bar.java foo.java
Which, I believe, will simply search for bar.java inside foo.java, ignoring the piped input from the point is that it is almost surely not what you want.ps
. But don't quote me on that -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With