I added posix_spawn_file_actions_t to your sample and confirmed on my machine that the output was redirected to / dev / null.
#include <sys/types.h> #include <stdio.h> #include <spawn.h> #include <unistd.h> #include <fcntl.h> int main(int argc, char ** argv) { posix_spawn_file_actions_t action; posix_spawn_file_actions_init(&action); posix_spawn_file_actions_addopen (&action, STDOUT_FILENO, "/dev/null", O_RDONLY, 0); pid_t pid; char *arg[] = {"execpath", NULL}; int status; extern char **environ; status = posix_spawn(&pid, "execpath", &action, NULL, argv, environ); posix_spawn_file_actions_destroy(&action); return 0; }
EDIT: MCVE sample added for full reference.
source share