• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to obtain PID in java  RSS feed

 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need PID(Process ID)of process on UNIX machine. Can anyone tell how to obtain in my program at runtime? I tried working with Runtime.exec() but it did not work out since exec method does not allow this.

Please guide ASAP

Thanks
Anju Sethi
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Runtime.exec to execute something like

ps aux | grep <my-process-name>

and then parse the result to get the PID. The syntax for ps on your Unix may differ a bit. Keep this in mind when using Runtime.exec.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but as per the doc even you referred
" Runtime.exec() is not a command line - One final pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments. In listing 4.6 below, a user tries to execute a command and redirect its output in one call to exec():" .

That's why - I can not use ps command .

Please guide
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you actually tried it? "ps" is an executable in Unix, not a command built into the shell, so you should have no problems. The pipe ("|") may not work, in which case you have to parse the full output of the ps command, but that's not much harder than parsing a single line.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it .
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!