主要通过posix_spawn实现
#include#include #include #include #include extern char **environ; void run_cmd(char *cmd) { pid_t pid; char *argv[] = {"sh", "-c", cmd, NULL}; int status; printf("Run command: %sn", cmd); status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ); if (status == 0) { printf("Child pid: %in", pid); if (waitpid(pid, &status, 0) != -1) { printf("Child exited with status %in", status); } else { perror("waitpid"); } } else { printf("posix_spawn: %sn", strerror(status)); } } int main(int argc, char* argv[]) { run_cmd("nohup /mnt/c/Ubuntu/3138/xxxxxsh "); return 0; }



