I want to know the owner of current process in Unix using Java. I want to find the current server's owner name. I tried with running "who am i" command in Runtime.getRuntime().exec(), but its not returning me any results.
String line = "";
Process p = Runtime.getRuntime().exec("who am i");
InputStream iStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(iStream);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
while ((line = bufReader.readLine()) != null) {
System.out.println("Input "+line);
}
Is there anything wrong with this code or any idea how can I find the owner of current process using Java?
getRuntimeMXBean(). getPid() method to get the process ID. This method returns the process ID representing the running Java virtual machine.
Fig: 'ps' command displaying all Java processes running on Linux machine. The red color highlight in the above figure indicates the process IDs of all Java processes running on this EC2 instance. From here, you can get hold of your application's process ID.
First thing, I think System.getProperty("user.name")
should work for that
Second thing, the reason your code is not returning anything is because the command is whoami
with NO SPACES so your exec line should be (assuming you are running on windows through cygwin or on a **nix based system)
Process p = Runtime.getRuntime().exec("whoami");
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