How to Find My Pid in Java or Jruby on Linux

How do I find my PID in Java or JRuby on Linux?

If you have procfs installed, you can find the process id via the /proc/self symlink, which points to a directory whose name is the pid (there are also files here with other pertinent information, including the PID, but the directory is all you need in this case).

Thus, with Java, you can do:

String pid = new File("/proc/self").getCanonicalFile().getName();

In JRuby, you can use the same solution:

pid = java.io.File.new("/proc/self").canonical_file.name

Special thanks to the #stackoverflow channel on free node for helping me solve this! (specifically, Jerub, gregh, and Topdeck)

How can a Java program get its own process ID?

There exists no platform-independent way that can be guaranteed to work in all jvm implementations.
ManagementFactory.getRuntimeMXBean().getName() looks like the best (closest) solution, and typically includes the PID. It's short, and probably works in every implementation in wide use.

On linux+windows it returns a value like "12345@hostname" (12345 being the process id). Beware though that according to the docs, there are no guarantees about this value:

Returns the name representing the running Java virtual machine. The
returned name string can be any arbitrary string and a Java virtual
machine implementation can choose to embed platform-specific useful
information in the returned name string. Each running virtual machine
could have a different name.

In Java 9 the new process API can be used:

long pid = ProcessHandle.current().pid();

In Java, is it possible to find out which port number is used by a known PID in UNIX systems?

you can use Runtime.getRuntime().exec

For Example the fallowing code shows how to get tcp socket of process with PID

2046

    public static void main(String[] args) {
// TODO Auto-generated method stub
Process p=null;
StringBuffer output = new StringBuffer();
String command= "netstat -p ";
try{
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";
while ((line = reader.readLine())!= null) {
if(line.matches("^tcp.*2406.*"))
output.append(line + "\n");

//reader.close();
}

} catch (Exception e) {
e.printStackTrace();
}

System.out.println(output);
}

output for this in my system was

tcp        0      0 192.168.2.239:52956     sc-in-f188.1e100.n:5228 ESTABLISHED 2406/chrome

we know the socket here is 52956 you can further parse the string for finding socket

refer

how-to-execute-shell-command-from-java

Alternative for spawning a process with 'fork' in jRuby?

I found out the solution for this. We can use the built-in library FFI in JRuby to 'simulate' the Process.fork in MRI.

# To mimic the Process.fork in MRI Ruby
module JRubyProcess
require 'ffi'
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function :fork, [], :int
end

pid = JRubyProcess.fork do
#internal_server.run
end

More details:

https://github.com/ffi/ffi

http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html

stop/start/restart java application from another java application

You can create a runnable jar of the program and START it from the controller using:
Runtime.getRuntime().exec("java -jar JAR-PATHNAME");

Inside your first app , just get the process id using the following statement which will return you the PID. ManagementFactory.getRuntimeMXBean().getName().substring(0,
ManagementFactory.getRuntimeMXBean().getName().indexOf("@")

Write this PID to a file and have the controller read it from the file. STOP can now be implemented using the kill command in linux ( Run it the same way you ran the java -jar commmand.



Related Topics



Leave a reply



Submit