finding java process id using perl on Linux

Whats the best way to find a java process for a particular user so that a system call for sighup can be issued? Basically I need to find the PID for several reasons - but the main one is it kill the jvm process from a tomcat installation. (There are more than one jvm running and  by having the user information I should be able to pull their active JAVA PID)

ps -u username | grep java works for bash.

In perl i tried:
system "ps -u username | grep java > tempPID.txt";

then i was going to access the file and pull the PID but I couldn't get the system call to work.

Any suggestions?

Thanks!
LVL 1
kipper3dAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TintinConnect With a Mentor Commented:
Assuming your system has pgrep (most Unix/Linux systems do)

my $pid=`pgrep -U username java` or die "Can not run pgrep $!\n";
0
 
kipper3dAuthor Commented:
Wow! I had no idea there was such a thing. Beautiful. Thank you!
0
 
Suhas .QA ManagerCommented:
on windows machine:

use module Win32::Process

1. $ProcessObj->GetProcessID()
2. Win32::Process::KillProcess($pid, $exitcode)
0
 
kipper3dAuthor Commented:
ok I tried this - finally.

The result:
5917 <- prints from Line 1
0 <- what is actually in $pid variable.

I need to carry the value 5917 in $pid to issue a kill statement but System returns 0
The statement above returns only a string.


my $pid = system("pgrep -U $user java");
 
print $pid;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.