Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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 ?
share|improve this question
add comment

6 Answers 6

When you close a terminal window, it sends a SIGHUP (hangup signal) to the shell, which then sends SIGHUP to all the processes it started. This is traditional behavior for bash and many other shells, and the traditional solution to this is to use nohup.

There are ways that different Unix machines can differ, so it's possible that the specific terminal emulators you used or the specific shells you used behaved different. But it's not specific to OS X. For example, there is a question on this same issue on Ubuntu.

share|improve this answer
add comment

Using the ampersand "&", you are telling Terminal to run the process in the background of the shell itself. Thus, when you close the shell (and kill the process), the GUI (Mail.app itself) will also close.

The correct command to launch Mail from the terminal is simply:

open -a Mail; exit

Edit: I just found this over at U&L Stack Exchange: What does ampersand mean at the end of a shell script line? The answers provided are excellent and explain exactly what's going on in more detail and better than I ever could! I highly recommend reading through it.

share|improve this answer
    
What about passing command-line params to the app ? Like this : /Users/nicolas/Desktop/Firefox-29-fr/Firefox.app/Contents/MacOS/firefox -profile "/Users/nicolas/Desktop/Firefox-29-fr/Profil-Firefox-29-fr/" -no-remote & –  Nicolas Barbulesco Jun 5 at 21:36
2  
@NicolasBarbulesco That is a separate question - see the man page for open and I think a question here –  Mark Jun 5 at 22:52
    
What about launching a specific application, not being in /Applications, but at a specific path ? Like this : /Users/nicolas/Desktop/Firefox-29-fr/Firefox.app/Contents/MacOS/firefox -profile "/Users/nicolas/Desktop/Firefox-29-fr/Profil-Firefox-29-fr/" -no-remote & –  Nicolas Barbulesco Jun 6 at 11:11
    
@NicolasBarbulesco open [full path] , or cd to the directory and open [file]. You can use ~/ for your home folder and the special character, "asterisk", as shorter notation assuming no other strings match the value, IE: ~/De*/Fire* ...et cetera. Or just write a simple script: cat > nameofscript ..... #! /bin/sh .... open [full path] .... ^D ..... chmod +x nameofscript..... .\nameofscript .... and optional, mv nameofscript $PATH/yourpersonalbin. Irecommend reading man open. It's a simple command, and as a simple command the tradeoff is it's not that powerful. –  njboot Jun 6 at 12:06
1  
Sorry, still need to use the -a argument: open -a Applications/Mail.app/Contents/MacOS/Mail; exit –  njboot Jun 6 at 14:35
show 2 more comments

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.

share|improve this answer
1  
This creates a file nohup.out in the current folder. Even when there is nothing to write. After a few weeks, I would have files nohup.out everywhere in my Mac. :-( –  Nicolas Barbulesco Jun 5 at 22:39
add comment

@jksoegaard has the proper commands but as you mentioned in the comment, it creates a nohup.out file. To stop the nohup.out file from being created, you need to redirect STDOUT and STDERR somewhere else. The complete command would be nohup program &>/dev/null &. This will run your program in the background, ignoring the SIGHUP, and sending all STDOUT and STDERR to /dev/null.

However, if you didn't remember to add nohup (like in your original question), you can use the disown -arh command to mark all backgrounded running jobs to ignore the SIGHUP.

share|improve this answer
    
Nice answer, yet still complicated. And nice profile image. :-) –  Nicolas Barbulesco Jun 6 at 11:18
add comment
up vote 0 down vote accepted

I have found the solution :

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

It is simple, it is pretty, and it works !

The parentheses ( ) launch the command in a sub-shell.

share|improve this answer
add comment

I have found a solution : fighting evil with evil.

I launch my app with this kind of command :

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

The shell exits at once. And the app survives, even after I close the Terminal window.

But this solution is not very handy.

share|improve this answer
1  
You really should be opening Applications that are bundled with open. That's future proof as bundle contents change as well as canonical. –  Ian C. Jun 5 at 23:09
    
@Ian — Here I am particularly interested in opening applications who are at a specific path, not in /Applications, and not bundled with my Mac. –  Nicolas Barbulesco Jun 6 at 11:33
1  
.app folders are called "bundles", and the open command can launch them whether they are in the /Applications folder or any other folder. The "bundle" term in this context does not mean "shipped with OS X". –  Andrew Medico Jun 6 at 16:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.