从守护进程中的exec()' ed进程读取stdout / stderr

时间:2016-08-15 17:34:21

标签: c++ c fork exec daemon

我有一个守护进程,我需要fork()/ exec()一些新进程,然后读取子进程stdout / stderr。

我的问题是父节点是stdout和stderr关闭的守护进程。反正有没有这样做?我必须打开一个外壳吗?

int status;
pipe(pipefd_stdout);
pipe(pipefd_stderr);

pid_t pid = fork();
if (pid == 0)
{   
    close(pipefd_stdout[0]);    // close reading end in the child
    close(pipefd_stderr[0]);    // close reading end in the child

    dup2(pipefd_stdout[1], 1);  // send stdout to the pipe
    dup2(pipefd_stderr[1], 2);  // send stderr to the pipe

    execvpe(cmd, (char**)args, (char**)env);
}
else
{
    // parent ...

    waitpid(pid, &status, 0);

    close(pipefd_stderr[1]);  
    close(pipefd_stdout[1]);  
}

0 个答案:

没有答案
相关问题