He probado con esto:
Código:
y con este otro:public String getPID() { String cmd = new String("echo $$") ; String PID = null; try { Process myProcess = Runtime.getRuntime().exec(cmd); BufferedReader stdout = new BufferedReader(new InputStreamReader(myProcess.getInputStream())); String line = stdout.readLine(); while ( line != null ) { System.out.println("Line: "+ line ); PID = line; line = stdout.readLine(); } int exitVal = myProcess.waitFor(); System.out.println( "exit value: " + exitVal ); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } return PID; }
Código:
Pero no hay manera ... La idea es obtener este pid cuando el hilo principal haga el start por tanto en el metodo run del hilo lo llamo:public String getPID() { Vector<String> commands=new Vector<String>(); commands.add("/bin/bash"); commands.add("-c"); commands.add("echo $PID"); ProcessBuilder pb=new ProcessBuilder(commands); Process pr=pb.start(); pr.waitFor(); if (pr.exitValue()==0) { BufferedReader outReader=new BufferedReader(new InputStreamReader(pr.getInputStream())); return outReader.readLine().trim(); } else { System.out.println("Error while getting PID"); return ""; } }
Código:
public void run() { pid = getPID(); .... }