Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ps –ef|grep java and ps –ef|grep *.java

Tags:

grep

unix

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
like image 492
hop Avatar asked Feb 17 '23 04:02

hop


2 Answers

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.

like image 69
Kent Avatar answered Mar 05 '23 08:03

Kent


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 ps. But don't quote me on that - the point is that it is almost surely not what you want.

like image 37
Dave S. Avatar answered Mar 05 '23 07:03

Dave S.