Running programs in the background from terminal
Solution 1
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:
setsid gnome-calculator
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:
gnome-calculator & disown
I also just learnt about spawning subshells with parenthesis. This simple method works:
(gnome-calculator &)
And of course there's nohup
as you mentioned. I'm not wild about nohup
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.
nohup gnome-calculator
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:
Solution 2
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
nm-applet &
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:
nm-applet & exit
Solution 3
As you pointed out, you can run
nohup nm-applet &
to ignore the end signal when closing the terminal. No problem with that.
Solution 4
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
bg
to send the running process into the background. Issue
fg
instead to put the running process back into the foreground.
EDIT: More detail in this answer I discovered later.
Solution 5
Use (exec PROGRAM &> /dev/null & )
to allow PID of subshell to be taken over by PROGRAM
. I've tested this approach multiple times with several different programs. Closing the original terminal has no affect on the newly-spawned program
Small demo:
$ # this is before running
$ (exec firefox &> /dev/null &)
$ # and look, we still in side the terminal and can continue working
Related videos on Youtube
OVERTONE
Updated on September 18, 2022Comments
-
OVERTONE almost 2 years
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.
-
OVERTONE over 12 yearsAny other alternatives? Just for knowings sake, not for anything else
-
desgua over 12 yearsAt wikipedia ( en.wikipedia.org/wiki/Nohup ) there is a suggestion to use
echo command | at now
which I couldn't get it to work. -
OVERTONE over 12 yearsI wonder how it works from a the UI when you double click an icon or program.
-
nutty about natty over 11 yearsOli, in the case of DISPLAY=:0 unity --replace which of the above would you use and why?
-
nutty about natty over 11 yearsAlso, in my command line "alarm clock"
sleep 10s && cvlc '/home/omm.ogg'
I've tried to add& disown
, but after the 10s when (c)VLC is launched, the process takes over the command line again. Is there an easy way to prevent that from happening? -
Oli over 11 years@nuttyaboutnatty wrap the whole lot in its own shell session:
sh -c "sleep 10s && cvlc '/home/omm.ogg'" & disown
. That's pretty much my solution for everything to make sure it forks out properly. -
Oli over 11 years@nuttyaboutnatty In answer to the older query, whatever works :) When I bug out to a TTY for relaunching a compositor (where I've lost Alt+F2), I'll run it from the TTY and then run it again from Alt+F2's run dialogue, effectively ending the TTY-run version... So it's not an issue. If you're having problems with output lingering on, you need to look at redirection which disowning et al won't affect that much.
-
nutty about natty over 11 yearsyou mean, run
DISPLAY=:0 unity --replace
first from a tty1, and then again from tty7? Why would you want to run it twice? In my (limited) experience, I'd run it once in tty1, then switch to tty7, back to tty1, back to 7 (without doing anything, only observing the successive changes as I switch to and fro), and finally, when tty7 has sorted itself out (after maybe a minute or so?) I'd one last time switch to tty1, hitctrl c
, exit, and switch back to tty7 (and reboot, depending on my mood and the constellation of the skies)... -
Oli over 11 years@nuttyaboutnatty That's what I mean but you wouldn't end up with two instances running because
--replace
does what it sounds like and actually ends the old instance. Running it the second time from within the xsession would end the copy bound to TTYn and would let you use that in the future if you needed it. There is probably a better way of doing it but just what comes to my mind each time I need it. -
Nikolaos Georgiou over 10 yearsPerfect, the one that worked for me was the subshell '(gnome-calculator &)'. I use it to launch a mono console app in the background. With the other techniques, the mono app would crash instantly.
-
7hi4g0 over 9 yearsThat's amazing! Never knew that and you seem to be the only person to mention this. Definitly that's my favorite approach when in a Desktop Environment. Too bad I can't upvote more ;p
-
kos over 8 yearsHmmm
(exec firefox)
hangs the terminal until I exit Firefox...and closing the terminal SIGHUPs the shell and the subshell -
David Foerster almost 8 yearsWelcome to Ask Ubuntu! I recommend editing this answer to expand it with specific details about how to do this. (See also How do I write a good answer? for general advice about what sorts of answers are considered most valuable on Ask Ubuntu.)
-
Sergiy Kolodyazhnyy almost 8 yearsNote to future users: creation of
nohup.out
can be suppressed with redirecting both stdout and sterr to/dev/null
like so:nohup firefox &> /dev/null &
-
Sergiy Kolodyazhnyy almost 8 yearsI've edited my answer. Tested it with several other programs. I've not observed firefox or other program hanging the controlling terminal in this version
-
Zanna almost 8 yearsworks well for me
-
MattKelly over 7 yearsI tried this with a different application and it failed:
matlab & exit
Running as two separate commands worked well:matlab &
exit
-
user1717828 about 7 yearsI was wondering why OP said closing the terminal caused his background programs to crash. I've never tried closing a terminal by the "x" button on the window.
-
Tim over 6 yearsthe x button is kill it doesn't cleanly exit, the exit command does. you could remap the x button to a exit command instead...
-
anonymous2 over 5 yearsAs this stands, it looks almost more like a comment than an answer. Please edit to expand. Don't just give a one-liner; explain why you right, ideally with citations. Answers without explanation are subject to removal.
-
sphere almost 5 years
exec
provides an alternative tonohup <command> &
for running a process in the background and returning current shell prompt. -
sybrek almost 5 yearsI don't believe that using bash's exec is necessary, it could be that you need the PID for some reason.
-
Admin about 2 yearsThis seems to be something you have developed, so please state that in your answer, other wise this is self-promotional spam. Also do this for your other answer, please.
-
Admin about 2 yearsPerfect for wireshark which needs sudo, thanks