2

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.

  • Run the app like so app & and it will detach from terminal – George Udosen Feb 25 '17 at 22:58
  • Without any special precautions, processes are owned by the session(terminal session). Hence, we need to disown the process or run it under a different application (like screen/tmux etc. I use tmux ). Check this question : http://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session – ankit7540 Feb 25 '17 at 23:05
  • @GeorgeUdosen That doesn't detach; it just runs asynchronously. – wizzwizz4 Apr 17 '18 at 18:01

2 Answers2

2

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]
L. D. James
  • 25,036
0

To explain it in simple words, when you run a script or application from terminal, usually you need to get some information about what it is doing. During this process, you are not able to run any other command. This is foreground process. But, there are cases in which you just want to run the app and it works behind the scene which is called Background process.

To start an application and send it to background, simply add & at the end of your command.

But if your app is already running, you can send it to background using Ctrl+Z. In this case, you can use fg command to take the process back to foreground.