MacOS – How does one start a bash command that doesn’t close on Terminal quit

bashmacosterminaltextedit

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.

Best Answer

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.