Ubuntu – (16.04) How to “detach” programs from terminal?

command linedefault-programs

I'm not sure what how or why this happens but sometimes when I open programs through terminal the terminal continues to attach itself to the program. I can understand this for things like "sudo nautilus" but for regular programs this is extremely annoying.

I cannot detach terminal from program because if I ctrl+c then it closes the program and if I quit the terminal then the program closes as well.

Few questions:

  • Why does this happen?
  • How do I manually activate/deactivate this function?
  • How do I detach the terminal from the program?

If I am asking too much then I understand. In that case I don't even know how to google the answer so if you can point me to any resources that talks about this or give me the name of the function that is happening that that would be great.

Thanks.

Best Answer

You'd best prepare for this ahead of time when starting the program.

There are are a number of options you can use when starting it. You can start it using the nohup option, whereas it would continue to run if you send it to the background and close the terminal.

There's the Screen option. Screen is an application. It isn't installed by default. You can install it with:

$ sudo apt install screen

You can then start a screen session by running screen at the terminal.

$ screen

While in screen you can bring up the help screen by hitting the keyboard shortcut: Ctl+A then hit ?. The Ctl+A puts screen into the listening mode for the a command to key to follow.

Applications started in a screen terminal will continue to run when you close the terminal. You can connect to the screen terminal on a different login... even from a different computer and continue with the application.

Screen has the detatch and reconnect functionality that you describe in your question.

Notes:

To detach a terminal running screen, just close it. To attach a terminal to a previous detached screen use these steps:

  • Find the screen you want to attach to:
$ screen -list
  • Use one of the attach options to attach to one from the list:
$ screen -D -r [desired detached screen]