Usually when we create a daemon in Unix/Linux, we need know the process id of the process in order to restart the process or kill it. And there’s no Java API for returning the JVM process ID. So this is a solution for getting the JVM process ID. Note that the example is only work for Unix-based OS only. I’ve implemented it on a BSD box.

First we need an application to output a UNIX systemcall getppid(). So i create getppid.c

main() {
  printf("%d", getppid());

After compiling it, lets create the Java code,


public class Pid {
  public static long getProcessID() {
    long pid = -1;
    try {
      Process p = Runtime.getRuntime().exec("./getppid");
      BufferedReader br = new BufferedReader(
        new InputStreamReader(p.getInputStream()));
      String t = null;
      while (null != (t = br.readLine())) {
        pid = Long.parseLong(t);
    } catch(Exception e) {
    return pid;

  public static void main(String[] args) {

Compile and run. Now whenever you call Pid.getProcessID() you’ll get the JVM process ID. Btw, you can replace the getppid.c with some shell script. For example:

echo $PID

Just make sure that the script has the execute permission: chmod +x