Fortunately, there is a way to free up your terminal and at
the same time still run long processes such as du.
You place the process in the background. A background
process is one that runs invisibly to you at the same time a different
process runs on your screen visible to you. The process visible
to you is running in the foreground. The
shell takes over the command line and places it in the background
when you follow the line with an &
metacharacter. For example, if you type:
$ du | sort > diskusage& [1] 6100 |
the second line is what the system returned: a job number
and a process number.
If the set -o monitor
option is on, (type set -o monitor
at the terminal to enable), a job sends a message to the terminal
upon completion of the form:
[1] + Done du | sort > diskusage& |
identifying the job by its number and showing that it has
completed, Done.
(For details, see Chapter 23 “Advanced Concepts and Commands”.)
Two commands enable you to manipulate jobs between background
and foreground: bg
and fg. bg
places a job in background; fg
pulls a background job into foreground (back to the terminal screen).
Suppose you had placed a job in the background using the &,
but now have decided to return it to the screen; type:
$ fg %job_number
or type %%
or %+ if it is
the current job. If it was the previous job, (meaning that you have
typed another command after placing the command in the background),
use %-.
The following example demonstrates how to brings a previous
command (du)
back to the foreground. The second background process (sleep
command) suspends execution of the shell for 999
seconds.
$ du | sort > diskusage& [1] 6100 $ sleep 999& [2] 6102 $jobs [2] + Running sleep 999 [1] - Running du | sort > diskusage $ fg %- du | sort >diskusage |
If you later decide you want your terminal free again, first
suspend the job, then type:
to put it back into the background.
You can also use these two commands on suspended jobs to restart
them in foreground or background.