Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Play Audio in Background?' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Play Audio in Background?
Authored by: corienti on Oct 03, '08 02:45:25PM

This is a little complicated - the full details are far too involved to go into right here.
However, the bottom line is that you can control whether a shell's subprocesses (bash = your shell) are terminated, when the shell is terminated.

By default, I don't think bash itself terminates its backgrounded children, when you exit bash (by typing control-D or exit).
That is, it does not terminate its children on my system, but I've long forgotten if that was something I configured myself, or the default behavior. I think it's default behaviour though - pretty sure.

However, from just testing now, it appears that if you click the red "close" button, Terminal itself terminates bash AND its children for you.

So the simple solution might be to close your shell by typing "exit" or control-D instead of clicking the close button.

What actually happens when you click the red "close" button is that the system sends a TERM (exit) signal to the shell - and when bash receives a TERM signal, it sends a TERM to all of its children before it exits itself.
However when you terminate bash by typing "exit" or control-D, it doesn't send a TERM to its children.

nohup and screen are not needed.
ksh also does not terminate its children by default when exiting, and nohup is needed.
Whether bash does or not is definitely configurable.

There's a lot I'm not explaining here but as you can see it would run into pages and pages and pages if I tried to explain it all here now!

If you read a few man pages (particularly of bash) and do a bit of testing you should be able to get it to do what you want.



[ Reply to This | # ]
Play Audio in Background?
Authored by: yogiken on Aug 05, '10 11:04:21AM
There is a simpler way to run a program in the background and detach it from the controlling terminal without using nohup. Just do this.

(afplay song.mp3&)

The parentheses request the creation of a subshell which becomes the parent process for the program you run. You can verify this works as follows.


(sleep 60&)
ps aux|grep sleep

You'll see something like this.


you      44389   0.0  0.0  2426564    324 s001  SN   11:00AM   0:00.00 sleep 60
you      44395   0.0  0.0  2425712    280 s001  R+   11:00AM   0:00.00 grep sleep

(Ignore the grep sleep which is the command you just ran.) A minute later, the same ps/grep will reveal that the program has terminated (and so has the parent subshell).

[ Reply to This | # ]