How does one start a bash command that doesn't close on Terminal quit?

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.


Solution 1:

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.

Solution 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

Solution 3:

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!