Reputation: 1440
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
Upvotes: 1
Views: 3156
Reputation: 74058
One possibility could be, that process A writes to stderr
instead of stdout
.
Then you must dup2(fd, 2)
instead.
If process A writes to stdout
and stderr
, you must dup2()
both:
if (dup2(fd, 1) < 0) {
// error handling
}
if (dup2(fd, 2) < 0) {
// error handling
}
Upvotes: 1