I have a program that forks(), and the child process is replaced by another process, say A, that is ran through invoking execv(A).
How do I redirect process A‘s output to /dev/null??
I’ve so far tried :
(The handle error parts are taken care of, and no error occurs)
pid = fork();
//check for errors
if (pid<0){
//handle error
}
//the child process runs here
if (pid==0){
fd = open("/dev/null", O_WRONLY);
if(fd < 0){
//hadnle error
}
if ( dup2( fd, 1 ) != 1 ) {
//handle error
}
if (execv(lgulppath.c_str(),args)<0){
//handle error
}
}
However, that, understandably doesn’t work , since it redirects the child process’s output to /dev/null and not process A‘s, later to replace the child, output.
Any ideas?
(I don’t have the code of A‘s process)
Thanks
One possibility could be, that process A writes to
stderrinstead ofstdout.Then you must
dup2(fd, 2)instead.If process A writes to
stdoutandstderr, you mustdup2()both: