Send messages from child process to parent

2019-09-06 20:41发布

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条回答
叛逆
2楼-- · 2019-09-06 21:05
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(.......);
}
查看更多
登录 后发表回答