mdevico mdevico - 1 year ago 99
Linux Question

launch process with fork and exec while redirecting stdout to /dev/null

I have a very specific problem for which I am unable to find the answer after numerous searches. I have a linux program. It's job is to launch another secondary executable (via

) when it receives a specific message over the network. I do not have access to modify the secondary executable.

My program prints all its TTY to stdout, and I typically launch it via
./program > output.tty
The problem I have is that this second executable is very verbose. It simultaneously prints to stdout while also putting the same TTY in a log file. So my
file ends up containing both output streams.

How can I set things up such that the secondary executable's TTY gets redirected to
? I can't use
because I can't afford to wait for the child process. I need to be able to fire and forget.


Answer Source

In the child process, before calling exec, you need to close the standard output stream.

pid_t pid =fork();
if (pid == 0) {
    // call exec
} else if (pid > 0) {
    // parent
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download