getting the process ID from inside UNIX. (echo problem)

F

Ferman

Hi,


I found a solution. I run a command by exec() on UNIX OS.

program part like this;
try
{
String vCommand({"echo", "$$"});
Process p = Runtime.getRuntime().exec(vCommand);
BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
int pid = Integer.parseInt(br.readLine());
System.out.println(pid);
p.destroy();
p = null;
br = null;
}
catch (java.io.IOException e)
{
System.out.println(e);
}

But it is give me "$$" characters. I didn't obtain the process id. What is
the problem ?
OS: SunOS
 
J

Joona I Palaste

Ferman said:
I found a solution. I run a command by exec() on UNIX OS.
program part like this;
try
{
String vCommand({"echo", "$$"});
Process p = Runtime.getRuntime().exec(vCommand);
BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
int pid = Integer.parseInt(br.readLine());
System.out.println(pid);
p.destroy();
p = null;
br = null;
}
catch (java.io.IOException e)
{
System.out.println(e);
}
But it is give me "$$" characters. I didn't obtain the process id. What is
the problem ?
OS: SunOS

The "$$" is a feature of the shell. However, processes run with
Runtime.exec() run directly on the OS, not in any shell. Therefore
your solution won't work. You could try executing a batch file that
executed "echo $$ >somefile" and then read somefile back in on the
Java side, but I'm not sure if that gets you the correct process ID.
For the really adventurous, write your own shell, which has some kind
of interface to return its process ID. Then call that interface from
Java via JNI or something.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
- Bart Simpson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top