Chapter 8. Process Management

Foreground and Background Jobs

Foreground and Background Jobs
Tag:

There are two types of jobs. One is a job running as it connects with a terminal – called a foreground job. The other is a job running in the background – called a background job. Understanding these concepts is important for managing multi-tasking jobs.

Foreground Job

When you run a command on a terminal, usually it is executed as a foreground job. As one terminal has only one command line, only one job can be running at the same time.

When you execute only a single command, usually the computer completes the process within a second. This makes it harder for us to see the difference between foreground jobs and background jobs. To see the difference clearly, try a practice task below.

Practice 1

Objective:
Understand how the foreground job works

1. Create a shell script

First, let's create a shell script that requires a longer processing time. The shell script (loop.sh file) below counts down numbers from 1,000,000.

Use vim to create a shell script file named loop.sh.

Command Line - INPUT
mkdir dir_ch8
cd dir_ch8
vim loop.sh

Press the i key to switch to the insert mode. Next, copy the code below and paste it into the file. After pasting the code, press the esc key followed by the : key and save the file by pressing the w + q keys. To learn how to use Vim, check Chapter 3. Vim Editor .

loop.sh
#!/bin/bash
  
count=5000000
while [ $count -ge 0 ]
do
echo "countdown $count"

count=$((count - 1))
done

2. Run the shell script

To run the shell script, adjust access mode and run the shell script with the file path.

Command Line - INPUT
sudo chmod u+x loop.sh
./loop.sh

When you run this as a foreground job, you'll see the countdown on your terminal. Until the countdown is completed, your command line is occupied and you cannot create another job.

Command Line - RESPONSE
:
countdown 920071
countdown 920070
countdown 920069
:

To suspend or stop the job, press Ctrl + Z or Ctrl + C.

Even when you redirect the standard output to a text file, the job is still running as a foreground job and you cannot make another job.

Command Line - INPUT
./loop.sh > countdown.txt

Background Job

As you cannot make a new job when a foreground job is running, if a program requires a lengthy process, it is good to run it as a background job. You can run multiple background jobs at the same time as they are running in the background. To start a background job, run a command with & at the end of the command.

Practice 2

Objective:
Understand how background job works

1. Run the shell script multiple times as a background job

Run the loop.sh file redirecting to a text file with & at the end. The command line gives you a number, which is the ID of the job.

Command Line - INPUT
./loop.sh > countdown_bg_1.txt &
Command Line - RESPONSE
[2] 83447

You can also execute the same command multiple times.

Command Line - INPUT
./loop.sh > countdown_bg_2.txt &
Command Line - RESPONSE
[3] 83450
Command Line - INPUT
./loop.sh > countdown_bg_3.txt &
Command Line - RESPONSE
[4] 83451

2. Check the job statuses

To see running jobs, run the jobs command, which will be explained in the next section. You can see that multiple jobs are running in parallel.

Command Line - INPUT
jobs
Command Line - RESPONSE
[1]   Running                 ./loop.sh > countdown_bg_1.txt &
[2]-  Running                 ./loop.sh > countdown_bg_2.txt &
[3]+  Running                 ./loop.sh > countdown_bg_3.txt &

There are two types of jobs. One is a job running as it connects with a terminal – called a foreground job. The other is a job running in the background – called a background job. Understanding these concepts is important for managing multi-tasking jobs.

Foreground Job

When you run a command on a terminal, usually it is executed as a foreground job. As one terminal has only one command line, only one job can be running at the same time.

When you execute only a single command, usually the computer completes the process within a second. This makes it harder for us to see the difference between foreground jobs and background jobs. To see the difference clearly, try a practice task below.

Practice 1

Objective:
Understand how the foreground job works

1. Create a shell script

First, let's create a shell script that requires a longer processing time. The shell script (loop.sh file) below counts down numbers from 1,000,000.

Use vim to create a shell script file named loop.sh.

Command Line - INPUT
mkdir dir_ch8
cd dir_ch8
vim loop.sh

Press the i key to switch to the insert mode. Next, copy the code below and paste it into the file. After pasting the code, press the esc key followed by the : key and save the file by pressing the w + q keys. To learn how to use Vim, check Chapter 3. Vim Editor .

loop.sh
#!/bin/bash
  
count=5000000
while [ $count -ge 0 ]
do
echo "countdown $count"

count=$((count - 1))
done

2. Run the shell script

To run the shell script, adjust access mode and run the shell script with the file path.

Command Line - INPUT
sudo chmod u+x loop.sh
./loop.sh

When you run this as a foreground job, you'll see the countdown on your terminal. Until the countdown is completed, your command line is occupied and you cannot create another job.

Command Line - RESPONSE
:
countdown 920071
countdown 920070
countdown 920069
:

To suspend or stop the job, press Ctrl + Z or Ctrl + C.

Even when you redirect the standard output to a text file, the job is still running as a foreground job and you cannot make another job.

Command Line - INPUT
./loop.sh > countdown.txt

Background Job

As you cannot make a new job when a foreground job is running, if a program requires a lengthy process, it is good to run it as a background job. You can run multiple background jobs at the same time as they are running in the background. To start a background job, run a command with & at the end of the command.

Practice 2

Objective:
Understand how background job works

1. Run the shell script multiple times as a background job

Run the loop.sh file redirecting to a text file with & at the end. The command line gives you a number, which is the ID of the job.

Command Line - INPUT
./loop.sh > countdown_bg_1.txt &
Command Line - RESPONSE
[2] 83447

You can also execute the same command multiple times.

Command Line - INPUT
./loop.sh > countdown_bg_2.txt &
Command Line - RESPONSE
[3] 83450
Command Line - INPUT
./loop.sh > countdown_bg_3.txt &
Command Line - RESPONSE
[4] 83451

2. Check the job statuses

To see running jobs, run the jobs command, which will be explained in the next section. You can see that multiple jobs are running in parallel.

Command Line - INPUT
jobs
Command Line - RESPONSE
[1]   Running                 ./loop.sh > countdown_bg_1.txt &
[2]-  Running                 ./loop.sh > countdown_bg_2.txt &
[3]+  Running                 ./loop.sh > countdown_bg_3.txt &
Tag: