问题
I am executing the parent code. I then do a fork and then execvpe. The new program that i "execvpe" throws a lot of console messages and i want to hide those.
Is it possible for me to redirect all my stdout and stderr messages in the child process onto a file?
I tried a close(1) so that i dont dump messages on console (stdout) and that didnt help
回答1:
pid_t pid = fork();
/* Child process */
if (pid == 0) {
/* Open log file */
int log = creat("logfile", 0644);
/* Redirect stdout to log file */
close(1);
dup(log);
/* Redirect stderr to log file */
close(2);
dup(log);
/* Execute other program: its stdout & stderr (1 & 2) will both point to logfile */
execvpe(.......);
}
来源:https://stackoverflow.com/questions/25315133/send-messages-from-child-process-to-parent