2

I want to run a command, and not have it quit when Terminal closes.

For instance, if I run top & ; disown, top quits when I quit Terminal. I want to keep top alive.

I know this is possible since the command open can do this. (For example, open -e opens up TextEdit, and when you quit out of Terminal, TextEdit stays open.)

Thanks in advance.

  • 1
    Why? There is nothing to see if the process isn't attached to a terminal/ – fd0 Sep 7 '16 at 20:24
  • top was just an example command. I would use a different command that doesn't output anything. – Flare Cat Sep 7 '16 at 20:26
  • I don't see any problem then. If the command doesn't output anything or accept any input then it should work. The Terminal.app may indicate that it is closing the process but the process continues to run after you quit the Terminal.app. Tested in OS X 10.6. – fd0 Sep 9 '16 at 13:29
3

Use screen:

To start a session and a process within run screen command (e.g. screen top). Detach from the session with ctrlActrlD.

Now you can close the Terminal session window or quit Terminal at all and the process started previously is still running.

After (re-)starting Terminal you can get a list of all detached sessions with screen -list. Use the pid to reattach to a session: screen -R [pid]. To stop the reattached session's process use the common commands (e.g. ctrlC for top) and exit to stop the session.

If you want to start a process in a new immediately detached session use: screen -d -m command (e.g. screen -d -m top). This will create a new screen session while you get a new prompt in your currently open shell session.

Please check man screen for a lengthy description, key bindings and customizations.

2

I think the key here is based on this statement:

For instance, if I run top & ; disown, top quits when I quit Terminal. I want to keep top alive.

The moment you quit Terminal, it immediately kills whatever you were running. This is where terminal multiplexers come into play.

The solution to this is to use tmux.

tmux is a "terminal multiplexer" but one of the best features about it is that it continues running even if you close Terminal.

From their man page:

tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

So, if you want to launch top, you could detach from it while it continued in the background even if Terminal is quit.

tmux is available through homeberew and MacPorts.

  • HomeBrew: sudo brew install tmux
  • MacPorts: sudo port install tmux

Detailed installation info can be found on their respective sites.

As for using tmux, just launch Terminal and execute tmux. You will get a Terminal screen with a green bar.

enter image description here

Execute (for example)top.

enter image description here

Quit and relaunch Terminal.

To get a list of sessions:

tmux list-sessions

0: 1 windows (created Wed Sep  7 18:13:21 2016) [132x24]

Attach to that specific session

tmux attach -t0 And the session comes back up.

enter image description here

0

When using a command you want to continue you would normally add something to the effect of:

nohup yourcommand &

The nohup stops it from receiving hang signals, and the & essentially runs it background so you can continue to do your own thing.

Hope that helps!

-1

Some programs are launched as a child process of the Terminal window, so closing it will close the program. In that case, launch such programs on a separate process accordingly:

  1. Enter the full path to the executable on the command line (/Applications/Safari.app/Contents/MacOS/safari)

  2. Use the open command and the appropriate flag, such as -a for app (open -a Safari), which launches applications in a separate process (pid). Once running, you can simply close the Terminal window and the application will remain open, as opposed to using direct executable execution by whatever means you are now using.

  3. The “osascript” command, which is OS X’s Terminal command for running “open scripting architecture” scripts like AppleScript (osascript -e 'open app "Safari"').

All three have a separate means of quitting the processes, but which are similar to each their respective means for starting them.

Keep in mind that these are not the only three, but rather the most arcane commands available to you. I supply them because I reason that anyone running commands in the terminal is already familiar with each and every stock command available in every standard UNIX/Linux/FreeBSD distribution, and readily knows how to use the relevant ones to spawn, kill, suspend, fork, resume, join and detach threads and processes—those being FreeBSD's sole reason for existence as the operating system installed on the machines of the world's most profitable company.

Anyway, one last thing: only the current user can run processes using the above commands; launch processes with these commands under a different user, you must enable the super user account (su), and then prefix each command with sudo.

You must log in to answer this question.

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