Linux Background Running

LinuxLinuxBeginner
Practice Now

Introduction

In the eerie silence of a long-abandoned sanatorium, you step into the tattered shoes of a nurse trapped in a haunting nightmare. Once a bustling haven of recuperation, the walls of this desolate institution now whisper with the echoes of its forgotten past. Emma, the nurse in your disturbing dream, is faced with a series of mysterious command-line tasks that hold the key to her escape. Your mission, should you choose to accept it, is to help Emma harness the power of Linux background processes to navigate the labyrinth of this chilling nightmare and unlock the secrets to freedom.


Skills Graph

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

Understanding Background Processes

In this step, Emma encounters her first challenge: launching a process that must continue running without her direct supervision, much like how the old sanatorium seems to linger with a life of its own. To achieve this, you will learn how to put command-line tasks in the background in Linux.

To place a task in the background, append & to the command. For instance, if you have a script that takes a long time to run, you would execute it like this:

./long-running-script.sh &

If she needs to verify that the process is running, Emma can use the jobs command.

Now, letโ€™s simulate this. Create a file called background-task.sh in the ~/project directory with the following content:

echo '#!/bin/bash' > ~/project/background-task.sh
echo '## Simulate a long-running process' >> ~/project/background-task.sh
echo 'sleep 30' >> ~/project/background-task.sh

Make the script executable and run it in the background:

chmod +x ~/project/background-task.sh
cd ~/project
./background-task.sh &

The result should be something like this:

[1] 12345

Here, [1] is the job number and 12345 is the process ID.

Managing Background Processes

In this step, Emma hears a distant, rhythmic beatingโ€”like a heart beating deep within the sanatorium. It mimics the soundtrack of countless background processes that keep the core of Linux alive. Your task is to manage these processes, bringing them to the foreground when needed, or pushing them deeper into the abyss of the background.

Letโ€™s create a new script called manage-bg-process.sh in the ~/project directory with the following code:

echo '#!/bin/bash' > ~/project/manage-bg-process.sh
echo '## Infinite loop to keep the script running' >> ~/project/manage-bg-process.sh
echo 'while true; do' >> ~/project/manage-bg-process.sh
echo '   echo "The heart of the sanatorium beats on..."' >> ~/project/manage-bg-process.sh
echo '   sleep 5' >> ~/project/manage-bg-process.sh
echo 'done' >> ~/project/manage-bg-process.sh

Run the script in the background and then bring it to the foreground with the job control commands fg:

chmod +x ~/project/manage-bg-process.sh
./manage-bg-process.sh &
jobs
fg %1

Replace %1 with the job number if it is different.

Summary

In this lab, we ventured into the realm of Linux background processes and job management. The lab was designed as an interactive, narrative-driven experience where you took on the role of a character to perform relevant tasks while immersed in a compelling story. Through a hands-on approach, you discovered how to run scripts in the background, keep track of them using jobs, and manage them with fg. This knowledge is essential for managing long-running tasks on a Linux system and is a critical skill for anyone working in a command-line environment.

Other Linux Tutorials you may like