Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 8. Process Management

Foreground and Background Jobs

Foreground and Background Jobs

Foreground and Background Jobs

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 &

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Efficient Access Mode Management with chmod Command

chmod Command with Numbers

Filtering Lists in Django Views

Customizing Views (2) – Filter Lists

Integrating Django Template Language (DTL)

Django Template Language (DTL)

Differences Between Websites and Django Web Apps

Websites vs. Django Web Apps

Managing User, Group, and Permissions in Linux

Chapter 4. User, Group and Permission

Efficient Access Mode Management with chmod Command

chmod Command with Numbers

Filtering Lists in Django Views

Customizing Views (2) – Filter Lists

Integrating Django Template Language (DTL)

Django Template Language (DTL)

Differences Between Websites and Django Web Apps

Websites vs. Django Web Apps

Managing User, Group, and Permissions in Linux

Chapter 4. User, Group and Permission

Tags:

Shell Script

Job

Foreground Job

Background Job

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server