10

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 ?
1

7 Answers 7

8

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.

2
  • 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. :-( Commented Jun 5, 2014 at 22:39
  • 1
    @NicolasBarbulesco Use either nohup command>/dev/null& or command&disown.
    – Lri
    Commented Sep 19, 2014 at 8:10
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.

6

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.

8
  • 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 & Commented Jun 5, 2014 at 21:36
  • 2
    @NicolasBarbulesco That is a separate question - see the man page for open and I think a question here
    – mmmmmm
    Commented Jun 5, 2014 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 & Commented Jun 6, 2014 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
    Commented Jun 6, 2014 at 12:06
  • 1
    Sorry, still need to use the -a argument: open -a Applications/Mail.app/Contents/MacOS/Mail; exit
    – njboot
    Commented Jun 6, 2014 at 14:35
4

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.

1
  • --and the & returns from the program to the shell without waiting, so you can keep using the shell or terminate it, without the program you started exiting. Commented Jan 11, 2017 at 18:20
3

@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.

1
  • Nice answer, yet still complicated. And nice profile image. :-) Commented Jun 6, 2014 at 11:18
0

Or you can try open -a /Applications/Mail.app

Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-s ][-b ] [-a ] [filenames] [--args arguments]
Help: Open opens files from a shell.
      By default, opens each file using the default application for that file.
      If the file is in the form of a URL, the file will be opened as a URL.
Options:
      -a                Opens with the specified application.
      -b                Opens with the specified application bundle identifier.
      -e                Opens with TextEdit.
      -t                Opens with default text editor.
      -f                Reads input from standard input and opens with TextEdit.
      -F  --fresh       Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
      -R, --reveal      Selects in the Finder instead of opening.
      -W, --wait-apps   Blocks until the used applications are closed (even if they were already running).
          --args        All remaining arguments are passed in argv to the application's main() function instead of opened.
      -n, --new         Open a new instance of the application even if one is already running.
      -j, --hide        Launches the app hidden.
      -g, --background  Does not bring the application to the foreground.
      -h, --header      Searches header file locations for headers matching the given filenames, and opens them.
      -s                For -h, the SDK to use; if supplied, only SDKs whose names contain the argument value are searched.
                        Otherwise the highest versioned SDK in each platform is used.
-2

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.

4
  • 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.
    Commented Jun 5, 2014 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. Commented Jun 6, 2014 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".
    – nobody
    Commented Jun 6, 2014 at 16:16
  • open is the same as clicking the icon. It's not the same a launching the app from the command line as the OP asks.
    – Ted Bigham
    Commented Jan 5, 2016 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .