0

我正在执行父代码。然后我做一个 fork 然后 execvpe。我“execvpe”的新程序抛出了很多控制台消息,我想隐藏这些消息。

我是否可以将子进程中的所有 stdout 和 stderr 消息重定向到文件中?

我尝试了关闭(1),这样我就不会在控制台(stdout)上转储消息并且没有帮助

4

1 回答 1

4
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(.......);
}
于 2014-08-14T19:07:21.550 回答