Suppose gedit is the program you want to run detached (aka. "disown", "disentangle", "decouple"). There are different ways depending on what you want to do exactly:
Program already running
Disown:
disown -h is the way to go if you want to do that with an already running program (i.e. if you forgot to nohup it). You first have to stop it using Ctrl+Z. Then you can put in in the brackground using bg [jobId] (e.g. bg 1). You get a list of running jobs with their jobId using jobs. After that you can decouple it from terminal using disown -h %[jobId]. Example terminal session:
confus@confusion:~$ gedit
^Z
[1]+ Stopped gedit
confus@confusion:~$ jobs
[1]+ Stopped gedit
confus@confusion:~$ bg 1
[1]+ gedit &
confus@confusion:~$ disown -h %1
confus@confusion:~$ exit
Program not started yet
nohup
nohup is not always present on all machines. If you know you want to decouple beforehand you would use:
nohup gedit &
Maybe you will want to redirect the shell output as well and your program a pseudo input source, so: nohup ./myprogram > foo.out 2> bar.err < /dev/null &. You would want to redirect the output to either not be annoyed by it or to use it later. The null-input can help to prevent hickups in ssh an such.
Subshell:
You can achieve a similar effect by
confus@confusion:~$ (geany 2>&1 /dev/null &)
The brackets open a new subshell to run gedit in. The 2>&1 /dev/null redirects the shell output to nowhere (suppressing the output). And the & at the end puts the process in the background.
Terminal multiplexing
Also terminal multiplexing using screen or byobu. You basically run the program in a terminal of its own. I can really recommend byobu for other reasons too. Below is a list of boybu-shortcuts that might come in handy for your first steps:
Useful:
- F2 Create a new window
- F3 Move to the next window
- F4 Move to the previous window
- F6 Detach from the session and logout
- Shift-F6 Detach from the session, but do not logout
- F7 Enter scrollback/search mode
- Ctrl-F5 Reconnect any SSH/GPG sockets or agents
Less useful:
Shift-F2 Split the screen horizontally --
Ctrl-F2 Split the screen vertically --
Shift-F3 Move focus to the next split --
Shift-F4 Move focus to the previous split --
Shift-F5 Collapse all splits --
F5 Refresh all status notifications --
F8 Rename the current window --
F9 Launch the Byobu Configuration Menu --
F12 GNU Screen's Escape Key --
Alt-Pageup Scroll back through this window's history --
Alt-Pagedown Scroll forward through this window's history --
Ctrl-a-! Toggle all of Byobu's keybindings on or off --
The 'at' deamon and others
at is a nice usefull little tool to run a command at a scheduled time. It can be 'misused' to detach a command from the shell:
echo './myprogram myoption1 myoption2' | at now
Also you can look into setsid and start-stop-daemon, but the other methods should suffice.
screen(mentioned by Oli and RobinJ). I am impressed by its capability; after reading about it and trying it out... It only requires the typing of:screen -d -m gedit(orscreen geditthenCtrl+a dto detach)... and I still have full access to gedit's terminal view (for warning messages etc) at any time viascreen -reven if I have closed the original terminal window in the meantime... – Peter.O Jun 4 '11 at 1:36