|
Home > Archive > Unix Programming > March 2004 > Redirecting the PID
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Redirecting the PID
|
|
| Dennis 2004-03-23, 10:36 am |
| Hi,
I'm attempting to build a pmfl type interface to manage some
processes. Such as monitoring their status, and having the option to
kill/restart them. I'm looking for a generic way to capture the PID
of each process that I'm running. Normally I would do something like:
system("sh -ic \"((program_executable > Debug &) 2>&1) >
/tmp/PID\" > /dev/null 2>&1");
This would redirect the debug output to a file called 'Debug' and
redirect the pid to a file called 'PID', which I can then read in.
However, now I want to be able to run the program inside an xterm so
that I can see the debug in realtime. Something like:
system("sh -ic \"xterm +wf -hold -e program_executable &\">
/dev/null 2>&1 > /tmp/PID");
The problem with this setup is that I successfully retrieve the pid
for the xterm, but I've been unable to get the pid for the
program_executable.
Does anyone have any ideas on how I might modify the above to return
BOTH the xterm PID to a file and the program_executable PID to another
file? Or perhaps you can think of a better way to get access to this
info. I thought of doing some "ps -ef | grep" type command to get the
PID, but thought I might run into some problems if I had multiple
processes running with the same name.
Thanks in advance for all of your ideas!
Dennis
| |
| Fletcher Glenn 2004-03-23, 11:39 am |
|
Dennis wrote:
> Hi,
> I'm attempting to build a pmfl type interface to manage some
> processes. Such as monitoring their status, and having the option to
> kill/restart them. I'm looking for a generic way to capture the PID
> of each process that I'm running. Normally I would do something like:
>
> system("sh -ic \"((program_executable > Debug &) 2>&1) >
> /tmp/PID\" > /dev/null 2>&1");
>
> This would redirect the debug output to a file called 'Debug' and
> redirect the pid to a file called 'PID', which I can then read in.
>
> However, now I want to be able to run the program inside an xterm so
> that I can see the debug in realtime. Something like:
>
> system("sh -ic \"xterm +wf -hold -e program_executable &\">
> /dev/null 2>&1 > /tmp/PID");
>
> The problem with this setup is that I successfully retrieve the pid
> for the xterm, but I've been unable to get the pid for the
> program_executable.
>
> Does anyone have any ideas on how I might modify the above to return
> BOTH the xterm PID to a file and the program_executable PID to another
> file? Or perhaps you can think of a better way to get access to this
> info. I thought of doing some "ps -ef | grep" type command to get the
> PID, but thought I might run into some problems if I had multiple
> processes running with the same name.
>
> Thanks in advance for all of your ideas!
>
> Dennis
For capturing program output directly, look at the command popen().
However, you cannot know the pid of a program run under the system()
command because when system returns, the program has already completed
execution. Probably the best alternative is to fork() and exec() the
program yourself, and create a pipe() connected to the program's stdin
and stdout yourself. This is how a shell program does this.
--
Fletcher Glenn
|
|
|
|
|