Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
5  
nohup 'command' & This seems to work. Any problems with this? –  OVERTONE Feb 22 '12 at 0:11
add comment

4 Answers

up vote 49 down vote accepted

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:

share|improve this answer
    
Oli, in the case of DISPLAY=:0 unity --replace which of the above would you use and why? –  nutty about natty Feb 7 '13 at 12:40
    
Also, 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? –  nutty about natty Mar 12 '13 at 14:44
3  
@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 Mar 12 '13 at 14:56
    
@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. –  Oli Mar 12 '13 at 15:01
1  
@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. –  Oli Mar 12 '13 at 15:25
show 2 more comments

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
share|improve this answer
add comment

As you pointed out, you can run

nohup nm-applet &  

to ignore the end signal when closing the terminal. No problem with that.

share|improve this answer
    
Any other alternatives? Just for knowings sake, not for anything else –  OVERTONE Feb 22 '12 at 0:38
    
At wikipedia ( en.wikipedia.org/wiki/Nohup ) there is a suggestion to use echo command | at now which I couldn't get it to work. –  desgua Feb 22 '12 at 0:45
    
I wonder how it works from a the UI when you double click an icon or program. –  OVERTONE Feb 23 '12 at 10:22
add comment

I can recommend the byobu terminal. You can easily detach your process by pressing the F6 key.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.