How do I run a program in the background of a shell, with the ability to close the shell while leaving the program running?
Lets say my UI is having problems or for some reason, I need to boot up a program from the terminal window, say, nm-applet
:
nm-applet
When it's started, it occupies the foreground of the terminal window.
Is there any simple way to run the program in the background without needing to leave the terminal open or have it occupy the whole terminal?
On that note, I did find a way to run programs from the terminal and have it allow for other inputs, by appending an ampersand (&
) to the command as such:
nm-applet &
But this isn't much use as any processes started in the terminal are killed once the terminal is closed.
I've recently come to like
setsid
. It starts off looking like you're just running something from the terminal but you can disconnect (close the terminal) and it just keeps going.This is because the command actually forks out and while the input comes through to the current terminal, it's owned by a completely different parent (that remains alive after you close the terminal).
An example:
I'm also quite partial to
disown
which can be used to separate a process from the current tree. You use it in conjunction with the backgrounding ampersand:I also just learnt about spawning subshells with parenthesis. This simple method works:
And of course there's
nohup
as you mentioned. I'm not wild aboutnohup
because it has a tendency to write to~/nohup.out
without me asking it to. If you rely on that, it might be for you.And for the longer-term processes, there are things like
screen
and other virtual terminal-muxers that keep sessions alive between connections. These probably don't really apply to you because you just want temporary access to the terminal output, but if you wanted to go back some time later and view the latest terminal activity, screen would probably be your best choice.The internet is full of
screen
tutorials but here's a simple quick-start:Here's the two ways I'd go with. Firstly, not running it from a terminal; hit Alt+F2 to open the run dialog, and run it from there (without &).
From a terminal, run
But do NOT close the terminal yourself. That is, do not hit the X-button to close, and do not use File -> Exit from its menubar. If you close the terminal that way, it will send a HUP (Hang UP) signal to the bash running within, which in turn will send the HUP signal to all its children (which is why nohup works in this case).
Instead, exit the shell by running
exit
or hitting Ctrl+D. bash will then disown its children, then exit, leaving the background processes still running. And when bash exits, the terminal has lost its child process, so it will close too.Doing it all at once:
As you pointed out, you can run
to ignore the end signal when closing the terminal. No problem with that.
One thing that many other answers are missing is how to detach a running process that currently blocks the shell. In most terminals and shells, Ctrl+Z will halt the running process and bring you back to an input prompt. Then, you can issue
to send the running process into the background. Issue
instead to put the running process back into the foreground.
EDIT: More detail in this answer I discovered later.
Use
(exec PROGRAM &> /dev/null & )
to allow PID of subshell to be taken over byPROGRAM
. I've tested this approach multiple times with several different programs. Closing the original terminal has no affect on the newly-spawned programSmall demo:
I can recommend the byobu terminal. You can easily detach your process by pressing the F6 key.
Although there are good answers above, I would like to give my 2 cents on how I use MATLAB in background.
The
-b
tag of sudo can run applications in background. You can also close the terminal after executing this command.Probably, unrelated but there is wonderful website that you can use to explain shell commands. http://explainshell.com/explain/8/sudo
I dont know its the right way but i just start another session while leaving the previous one allone. For example i ran a simple web server on my raspberry. the web.py one. then i start a new session while leaving it alone. thats it. it also is wuite useful since you are still updated even though you are workin on the other session.
In case KDE is used, you can also use
kstart
, which will start your program detached from the terminal. It also makes sure that the KDE environment is correctly setup for the command. (Seekstart.cpp
source code for reference. As you see from there, it usesKProcess::startDetached
, andKProcess
is derived fromQProcess
, andstartDetached
starts a new process, and detaches from it.)Similar is also
kde-open
orxdg-open
orgnome-open
.The modern and easy to use approach that allows managing multiple processes and has a nice terminal UI is hapless utility.
Install with
pip install hapless
(orpython3 -m pip install hapless
) and just runSee docs for more info.
NOTE: I'm the author of this package.