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).
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:
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.
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:
There are multiple answers here, depending on what you want (this answer is valid in
zshshells, others may vary).
If you need to run a command in background and you know it before running it, simply add a
&at the end of the command (using
sleep 60, do nothing during 1 minute, as example command):
If you have already run it, you can stop it with ctrl-Z, and when the shell gives you a prompt, you can background it with the command
In both cases, the process/job is still attached to your terminal; if you close your terminal a hangup (HUP) signal is sent to the process --- most process will gracefully exit then. If you need to ensure that the process will continue, you can either start it with:
or, after having sent it to background with
bgor with a simple
&, tell the shell to forget about it, with:
%%is a job control shortcut, and here stands for the last process sent in background).
Then you have to take account of the output of the process --- in the first two cases the output will still arrive to the terminal; in the case of
nohupit will be diverted on a file called
nohup.out, and in the latter case (with
disown) it will go to the terminal unless you close it, in which case the behavior is quite undefined. It is good practice to take care yourself of the output of a background process using redirection.