Process run via posix_spawn() results in defunct process immediately

135 Views Asked by At

Thank you in advance.

When I try to launch a cli-based music player via bash, all is well and the player (mplayer) works with a simple path passed on the command line (/home/pi/Music/*).

However, when I run the same player from another app via posix_spawn(), it seems to immediately become defunct:

pi        7974  7973 51 21:45 pts/0    00:00:05 [mplayer] <defunct>

I want to retain the pid of the spawned player (so I can terminate it later) but I don't want the parent process to wait, either.

What could I possibly be missing, please?

Code follows:

#define PLAYERPATH "/usr/bin/mplayer"

//...
char *argV[] = {PLAYERPATH, "-really-quiet", "-shuffle", "-loop 0", "/home/pi/Music/*", (char *) 0};

I expected the mplayer application to simply start playing music, I suppose.

1

There are 1 best solutions below

1
pktm On

I'm not entirely sure why this makes a difference (though I'd love an explanation), but what made the difference here was not spawning mplayer directly, but rather spawning /bin/sh and letting it run mplayer as follows:

char *argV[] = {"sh", "-c", "/usr/bin/mplayer -nogui -really-quiet -loop 0 -shuffle /home/pi/Music/*.mp3", (char *) 0};

//...

status = posix_spawn(&processID, "/bin/sh", NULL, NULL, argV, environ);

Leaving this here in the hopes it will be helpful to others.