Linux Background Management

LinuxLinuxBeginner
Practice Now

Introduction

In the midst of a fierce 'Desert Storm', where relentless winds howl and sand covers the sky, lives a mysterious 'Desert Wizard'. His ancient and enigmatic tent is an oasis of technology amidst the vast, barren landscape. Inside, amidst various arcane artefacts and mystical scrolls, is a series of computer systems running Linux, harnessing the power of background processes to perform long-running magical calculations and data analysis.

The Desert Sorcerer, known for his mastery of Linux, offers you a challenge. He tasks you with managing and mastering Linux background processes to keep the tent's computational rituals running smoothly, while ensuring that the ever-important resource of the 'foreground terminal' remains available for immediate and reactive spellcasting.

The aim of this scenario is to take you through a series of tasks that will give you the skills to efficiently manage background processes on a Linux system. Get ready to unlock the secrets of the desert and become an adept in the art of Linux background management.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/bg_process -.-> lab-271231{{"`Linux Background Management`"}} end

Starting a Process in the Background

In this step, you'll learn how to start a process in the background, allowing you to continue working in the terminal without waiting for the process to complete.

Firstly, ensure you are in the correct working directory:

cd ~/project

Now, let's say you have a script that performs a lengthy operation. We'll simulate this with a simple bash script. Create a file named long_running.sh :

First, type the following command to create a new script file and enter edit mode:

vim ~/project/long_running.sh

Then, once in Vim's edit mode, press the letter i (insert mode) to start editing the contents of the script.

#!/bin/bash
sleep 15
echo "Process completed"

Press Esc to exit insert mode.

To exit Vim, press Esc followed by the letter : (colon), then type wq (lowercase), and press Enter.

Make the script executable:

chmod +x long_running.sh

Now you can start this script in the background by appending an & to the command:

./long_running.sh &

You should see an output similar to this, which includes the Job ID and Process ID:

[1] 12345

You can continue with other tasks while long_running.sh is running in the background.

Managing Background Processes

After starting processes in the background, it's important to know how to manage them. In this step, you'll learn with the help of bg, and fg commands.

Imagine you started a process in the foreground accidentally and want to move it to the background. You can pause the foreground process using Ctrl + Z and then use bg to resume it in the background.

Let's try this by running the long_running.sh script without the &:

./long_running.sh

Immediately press Ctrl + Z to suspend the process. You'll see an output like this:

^Z
[1]  + 1650 suspended  ./long_running.sh

To resume the process in the background, type:

bg %1

You'll see an output like this:

[1] - 1650 continued ./long_running.sh

And if you need to bring it back to the foreground, use:

fg %1

You'll see an output like this:

[1] - 1650 running ./long_running.sh

Summary

In this lab, we journeyed through the essence of Linux Background Management, a crucial skill in the realm of computing wizards. We began with an engaging narrative, summoning the intrigue of a desert storm and a mysterious wizard to frame our tutorial. We learned to start a process in the background, manage running background processes, and seamlessly switch between foreground and background tasks.

This lab was created with the intention of imparting practical hands-on experience to novices. By adhering to a step-by-step approach, learners can build confidence in managing Linux background processes, a fundamental aspect of multitasking. The Desert Wizard's challenge was met with success, and you, the learner, have unlocked a new level of mastery in the digital expanse.

Thank you for embarking on this adventure, and may your new skills empower you in the vast, sandy realms of your computing journey.

Other Linux Tutorials you may like