How to get PID by process name?

You can get the pid of processes by name using pidof through subprocess.check_output: from subprocess import check_output def get_pid(name): return check_output([“pidof”,name]) In [5]: get_pid(“java”) Out[5]: ‘23366\n’ check_output([“pidof”,name]) will run the command as “pidof process_name”, If the return code was non-zero it raises a CalledProcessError. To handle multiple entries and cast to ints: from subprocess import … Read more

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 … Read more