Execv query

I have written a daemon which closes all std file descriptors. Now this deamon fork-exec's a shell script. Since the script has a number of echo's and i need a way to re-direct it to /dev/null

Since this seems to change a deal of scripts , i was hoping to re-direct the output by setting process parameters to /dev/null. Is this possible with one of
the exec functions ?

Opening
char *argsa[] = {"/usr/local/script.sh", ">","/dev/null","2>&1",(char *) 0 };
execReturn = execv(SCRIPT_PATH.c_str(),argsa);

This is failing.....

Any suggestions ?

Cheers!
Redirections are handled by the shell.

Assuming you have forked() already, then before your execv() in the child:

1
2
3
4
close( 1 );
close( 2 );
open( "/dev/null", O_WRONLY );  // should return 1
open( "/dev/null", O_WRONLY ); // should return 2 


now when you exec, stdout and stderr of the script.sh will be redirected
to /dev/null.
Thanks jsmith ! Wonderful solution...
Topic archived. No new replies allowed.