- [Narrator] When we start programs from the shell,
they usually stay as interactive tasks in the foreground.
This way, we see the output on the screen,
and we can terminate them quickly
by pressing Control + C.
However, there are times when putting a current task
in the background would be advantageous.
Whenever we want to check up on a task,
we could bring it to the foreground again.
We can do this with a couple of commands.
To set this up, let's open two terminals.
Go to Applications, Favorites, Terminal,
and drag this terminal to the right border.
Bump the font size and then open another terminal.
Applications, Favorites, Terminal,
drag this terminal to the left border
and bump its font size as well.
We will use the ps command to keep an eye
on nadd commands running.
Normally we'd run top for realtime tasks,
but we're going to combine ps with a watch command.
Watch executes a command every two seconds until terminated.
By using the watch command, we can run ps often
and watch the updates.
This keeps the amount of data on the screen to a minimum.
In the righthand terminal, type in
watch ps -C dd --format pid,cmd,%cpu
and hit Enter.
This will run the ps command every two seconds,
only show dd command processes,
and output the process id, the command name,
and its CPU usage.
In the lefthand terminal, we'll run the dd command.
Type in dd if=/dev/zero
of=/dev/null and hit Enter.
dd will read zeroes from /dev/zero
and output them to /dev/null
where they'll be discarded.
This will use up quite a lot of CPU time
but will allow us to see how process jobs work.
After running the dd command,
we can see that our terminal is busy.
If we press Control + C, it will terminate the process
which isn't what we want.
Let's press Control + Z in the left terminal instead.
The message on the screen is
that our dd process is stopped,
and we get our prompt back.
Check the CPU usage in the righthand terminal
and watch it slowly fall.
Our dd command wasn't terminated.
It was just put in the background and stopped.
It takes a while for it to wind down, as you can tell.
Now in the lefthand terminal, type in jobs and hit Enter.
The output shows our one job which is the dd command.
Inside the square brackets is the job spec number.
We can start this process back up
and either run it in the background
or run it in the foreground.
Let's run it in the background by typing in bg 1
and hit Enter.
The number one is the job spec number
from the output of jobs.
When we only have one job,
we don't actually have to specify the job spec number.
We're doing it here to be thorough.
Now verify it by typing in jobs again.
We can see that it is now running again,
and we have our prompt back.
The information in the right terminal
will show that it's picking up speed again.
If we want to bring the background process
to the foreground, just type in fg
into the lefthand terminal.
This will bring it back into the foreground
so we can interact with it again.
You can place a process in the background
as many times as you want by pressing Control + Z.
Just don't forget to type in bg,
or your process will be sitting there not doing anything.
Now that our process is back in the foreground,
type in Control + C to terminate it.
As soon as we terminated it, dd disappears
from the righthand terminal.
In this exercise, we placed the dd command
in the background by pressing Control + Z
because it was already running.
We can, however, start processes in the background
by appending an ampersand to the command line.
In the lefthand terminal, bring the dd line back up.
Go to the end of the line and append an ampersand
and then hit Enter.
You'll notice that we immediately get our prompt back,
and we can see it running
by viewing the righthand terminal.
To terminate dd, we could either use the fg command
to bring it to the foreground followed by a Control + C,
or we could type in killall dd and hit Enter.