The "background jobs" facilities in
bash (that is, your command-prompt) are very useful. Here are a few cool things you can do: (See:
info bash.)
(1) When you start any program,
bash launches the program and waits for it to complete. If you end the command with '&' then it launches the program and does
not wait for it to complete. It is a background job. The command
jobs will show you all of the jobs known to the shell. Commands
fg and
bg let you switch any of them to the foreground.
(2) Pressing
Ctrl-Z will stop (suspend) the foreground job, returning you to the shell prompt. You can send the job to the background or resume it in the foreground as you wish.
(3) When you log-off the computer,
bash sends a
signal to all jobs, unless you used the
disown command on them. This signal is SIGHUP. The
nohup command causes the program to ignore the signal. Ordinarily the signal causes a program to die.
(4) The
nice command allows you to "be nice" to the system and to other users by running a program at lower priority. If a program is resource-intensive, that is "nice" to do. There's also a
renice command.
(5) When you log back on to the computer, the
jobs command won't list programs that were left behind when you logged off, since
this shell does not own them. But the
ps command will show that the process still exists (if it still does).
(6) If you want to capture the output of a background job, you should divert the output to a file. For example, I had to rebuild X-windows recently, so I did this:
nice nohup make World >WorldLog.txt 2>WorldErrs.txt to say:
- nice: run this work at reduced priority.
- nohup: don't die when I log off and go to bed.
- make World: the command to do the rebuild.
- >WorldLog.txt: write "normal" output to this file.
- 2>WorldErrs.txt: write "error" output to this file.