PROBLEM:
Sometimes you want to start a process and forget about it. If you start it from the command line, like this:
[pastacode lang=”bash” manual=”redshift%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
[ad type=”banner”]
you can’t close the terminal, or it will kill the process. Can you run a command in such a way that you can close the terminal without killing the process?
SOLUTION 1:
close a terminal without killing the command running in it
One of the following 2 should work:
[pastacode lang=”bash” manual=”%24%20nohup%20redshift%20%26%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
OR
[pastacode lang=”bash” manual=”%24%20redshift%20%26%0A%24%20disown%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
SOLUTION 2:
If your program is already running you can pause it with Ctrl-Z, pull it into the background with bg and then disown it, like this:
[pastacode lang=”bash” manual=”%24%20sleep%201000%0A%5EZ%0A%5B1%5D%2B%20%20Stopped%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sleep%201000%0A%24%20bg%0A%24%20disown%0A%24%20exit%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
SOLUTION 3:
The reason that the process is killed on termination of the terminal is that the process you start is a child process of the terminal. Once you close the terminal, this will kill these child processes as well.
You can see the process tree with pstree, for example when running kate & in Konsole:
[pastacode lang=”bash” manual=”init-%2B%0A%20%20%20%20%20%E2%94%9C%E2%94%80konsole%E2%94%80%E2%94%AC%E2%94%80bash%E2%94%80%E2%94%AC%E2%94%80kate%E2%94%80%E2%94%80%E2%94%802*%5B%7Bkate%7D%5D%0A%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%E2%94%94%E2%94%80pstree%0A%20%20%20%20%20%E2%94%82%20%20%20%20%20%20%20%20%20%E2%94%94%E2%94%802*%5B%7Bkonsole%7D%5D%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
To make the kate process detached from konsole when you terminate konsole, use nohup with the command, like this:
[pastacode lang=”bash” manual=”nohup%20kate%20%26%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
After closing konsole, pstree will look like this:
[pastacode lang=”bash” manual=”init-%2B%20%0A%7C-kate—2*%5B%7Bkate%7D%0A%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
[ad type=”banner”]
and kate will survive.
An alternative is using screen/tmux/byobu, which will keep the shell running, independent of the terminal.
SOLUTION 4:
You can run the process like this in the terminal
[pastacode lang=”bash” manual=”setsid%20process%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
This will run the program in a new session
SOLUTION 5:
use a variant on screen,the command like this
[pastacode lang=”bash” manual=”screen%20bash%20-c%20’long_running_command_here%3B%20echo%3B%20read%20-p%20%22ALL%20DONE%3A%22’%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
The session can be disconnected with Ctrl A Ctrl D and reconnected in the simple case with screen -r. you have this wrapped in a script called session that lives in my PATH ready for convenient access:
[pastacode lang=”bash” manual=”%23!%2Fbin%2Fbash%0A%23%0Aif%20screen%20-ls%20%7C%20awk%20’%241%20~%20%2F%5E%5B1-9%5D%5B0-9%5D*%5C.’%22%241%22’%2F’%20%3E%2Fdev%2Fnull%0Athen%0A%20%20%20%20echo%20%22WARNING%3A%20session%20is%20already%20running%20(reattach%20with%20’screen%20-r%20%241′)%22%20%3E%262%0Aelse%0A%20%20%20%20exec%20screen%20-S%20%22%241%22%20bash%20-c%20%22%24%40%3B%20echo%3B%20echo%20′——————–‘%3B%20read%20-p%20’ALL%20DONE%20(Enter%20to%20exit)%3A’%22%0A%20%20%20%20echo%20%22ERROR%3A%20’screen’%20is%20not%20installed%20on%20this%20system%22%20%3E%262%0Afi%0Aexit%201%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
This only works when you know in advance you want to disconnect a program. It does not provide for an already running program to be disconnected.
SOLUTION 6:
prefer:(applicationName &)
Example:
[pastacode lang=”bash” manual=”%20%20%20%20%20%20%20%20%20%20%20linux%40linux-desktop%3A~%24%20(chromium-browser%20%26)%0A” message=”BASH CODE” highlight=”” provider=”manual”/]
[ad type=”banner”]
Make sure to use parenthesis when type the command!
SOLUTION 7:
You can set a process (PID) to not receive a HUP signal upon logging out and closing the terminal session.
Use the following command:
[pastacode lang=”bash” manual=”nohup%20-p%20PID%0A” message=”BASH CODE” highlight=”” provider=”manual”/]