MacOS – How to prevent the application launched in Terminal from undesirably exiting

applicationscommand linemacosterminalunix

I launch applications from the Terminal with commands like this one :

/Applications/Mail.app/Contents/MacOS/Mail &

The important part is the &.

  • For Unix / Linux / Solaris :

    • I have learned at school that adding the & makes the program live on his own.
    • I strongly remember launching a command like xeyes & and having the program running safe even after I close the shell.
  • On Mac OS X (Unix inside) :

    1. I launch an app with & and I close the Terminal window : the application exits !
    2. Why does this undesirable behaviour happen on Mac OS X ?
    3. How can this be fixed ?

Best Answer

The "standard" way of accomplishing what you want to accomplish (on OS X as well as on Linux, FreeBSD or other systems) is to use the command nohup:

nohup program &

This will start program, which will run in the background relative to the shell because of the & - and will ignore hangup signals due to the nohup command.

This way the program will continue running even though you close the shell. It doesn't matter if you're closing the shell because you're closing Terminal.app, or you're closing the shell because you are disconnecting from a ssh connection to the computer, or similar.