0

I am trying to get ProcessId of the same process from which I am running the pgrep command. But the InputStream is returning null.

If we run command pgrep terminal from terminal in mac we cant get the processid of terminal. but if we run lsappinfo command in terminal and search for terminal it is there along with the processid.

I want to get processId of the same process from which I am running the command, Is there any other efficient way to get processId from process name like that of pgrep.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Nirman
  • 103
  • 2
  • 11
  • You mean you're trying to get your java program's pid? – Federico klez Culloca Jul 26 '19 at 10:36
  • yes, by calling pgrep from the same program using ProcessBuilder. – Nirman Jul 26 '19 at 11:28
  • Then you might be interested in [this answer](https://stackoverflow.com/a/35885/133203), particularly the part on how it's done since Java 9. – Federico klez Culloca Jul 26 '19 at 12:10
  • thanks, Federico but I need a solution in Java 8, and also i am not able to understand why can't we get the process id of the same process from it (For example : if you run `pgrep terminal` it will return null. even if that process has a pid, which we can get by `lsappinfo` and search terminal in it you can see the processid along with it). – Nirman Jul 26 '19 at 17:46

0 Answers0