Introduction
Linux provides powerful capabilities for managing processes, allowing users to run commands in the background while continuing to work in the terminal. This functionality is essential when dealing with time-consuming tasks that don't require constant monitoring. In this lab, you will learn how to run processes in the background, check their status, and manage them effectively. These skills are fundamental for any Linux user and provide greater flexibility when working in command-line environments.
Understanding Background Processes
In Linux, processes can run in either the foreground or background. A foreground process is one that occupies your terminal until it completes, preventing you from running other commands. A background process, however, runs without blocking your terminal, allowing you to continue using the command line.
Let's first create a simple script that simulates a long-running task. This will help us demonstrate background processes.
Navigate to the project directory:
cd ~/project
Create a file called long-task.sh using the nano editor:
nano long-task.sh
Add the following content to the file:
#!/bin/bash
## This script simulates a long-running task
echo "Starting long task..."
for i in {1..20}; do
echo "Processing step $i..."
sleep 5
done
echo "Long task completed."
Press Ctrl+O followed by Enter to save the file, then Ctrl+X to exit nano.
Make the script executable by changing its permissions:
chmod +x long-task.sh
Now, run the script in the foreground:
./long-task.sh
You'll see the script outputs each step with a 5-second delay. Notice that during this time, you cannot enter other commands in the terminal because the process is running in the foreground.
Press Ctrl+C to terminate the script before it completes.
Running Processes in the Background
To run a process in the background, simply append an ampersand (&) at the end of the command. This tells the shell to execute the command in the background.
Let's run our long-task.sh script in the background:
cd ~/project
./long-task.sh &
You should see output similar to the following:
[1] 1234
Starting long task...
Processing step 1...
Here, [1] is the job number assigned by the shell, and 1234 is the process ID (PID). The job number is used to refer to this background process within the shell, while the PID is used by the operating system to identify the process.
Notice that after the initial output, you get your command prompt back. You can now continue to enter other commands while the script runs in the background.
For example, try creating another file:
echo "I can do other work while the script runs" > background-demo.txt
cat background-demo.txt
You'll see that you can continue working in the terminal while occasionally seeing the output from the background script. This is very useful for running time-consuming tasks without blocking your terminal session.
Important Note: It's completely normal to see the output from the background process appearing in your terminal. When you run a process in the background using &, the process executes in the background, but its output streams (stdout and stderr) are still connected to your terminal. This means:
- The process is running in the background (you can enter other commands)
- The process continues to execute without blocking your terminal
- The output still appears on your screen (which might mix with your other commands)
This behavior is expected and demonstrates that the background process is actively running and producing output.
Checking Background Processes
Linux provides several commands to check which processes are running in the background. The two most common ones are jobs and ps.
Using the jobs Command
The jobs command shows the status of all background jobs in the current shell session:
jobs
You should see output similar to:
[1]+ Running ./long-task.sh &
The + sign indicates that this is the current default job, which means it will be operated on if you use commands like fg or bg without specifying a job number.
Using the ps Command
While jobs only shows processes started from the current shell, the ps command can show all processes running on the system:
ps aux | grep long-task.sh
The output should include a line for your background script:
labex 1234 0.0 0.0 4508 1996 pts/0 S 12:34 0:00 /bin/bash ./long-task.sh
Let's create another background process so we can see multiple jobs:
cd ~/project
nano another-task.sh
Add the following content:
#!/bin/bash
## This script simulates another task
echo "Starting another task..."
for i in {1..20}; do
echo "Another task - step $i..."
sleep 5
done
echo "Another task completed."
Save and exit nano (Ctrl+O, Enter, Ctrl+X), then make the script executable:
chmod +x another-task.sh
Run this script in the background as well:
./another-task.sh &
Now check your background jobs again:
jobs
You should see both jobs listed:
[1]- Running ./long-task.sh &
[2]+ Running ./another-task.sh &
Managing Background Processes
Now that we have processes running in the background, let's learn how to manage them. Linux provides several commands to control background processes:
Bringing a Background Process to the Foreground
The fg command brings a background process to the foreground. If you have multiple background jobs, you can specify which one by using its job number.
Let's bring our first job to the foreground:
fg %1
The %1 refers to job number 1. You'll see the output of the long-task.sh script now appearing in your terminal, and your command prompt will be unavailable until the process completes or you stop it.
To interrupt the process and return to the command prompt, press Ctrl+C.
Sending a Foreground Process to the Background
You can also send a foreground process to the background. First, suspend the foreground process with Ctrl+Z, then use the bg command to continue it in the background.
Start a new foreground process:
cd ~/project
./long-task.sh
After a few seconds, press Ctrl+Z to suspend the process. You'll see a message like:
[1]+ Stopped ./long-task.sh
Now, continue it in the background:
bg
The process will resume running in the background, and you'll see:
[1]+ ./long-task.sh &
You can verify it's running with:
jobs
Terminating Background Processes
To terminate a background process, you can use the kill command with the job number or process ID:
kill %1
Or using the PID (replace 1234 with the actual PID from your ps output):
kill 1234
To verify the process has been terminated:
jobs
You might see a message indicating the job was terminated:
[1]+ Terminated ./long-task.sh
Running Processes Beyond Logout
Sometimes you need a process to continue running even after you log out of the terminal. The nohup command allows you to run a command that will continue running even after you exit the shell.
Let's create a script that will run for a long time:
cd ~/project
nano persistent-task.sh
Add the following content:
#!/bin/bash
## This script runs for a very long time
echo "Starting persistent task at $(date)" > persistent-output.log
for i in {1..100}; do
echo "Iteration $i at $(date)" >> persistent-output.log
sleep 10
done
echo "Persistent task completed at $(date)" >> persistent-output.log
Save and exit nano, then make the script executable:
chmod +x persistent-task.sh
Now, run it with nohup and put it in the background:
nohup ./persistent-task.sh &
You'll see output similar to:
[1] 5678
nohup: ignoring input and appending output to 'nohup.out'
This means the process is running in the background and will continue even if you log out. Output that would normally go to the terminal is redirected to a file called nohup.out.
Tip for Managing Output: If you don't want the background process output to appear in your terminal and potentially interfere with your work, you can redirect the output to a file:
## Redirect output to a specific file
./long-task.sh > task-output.log 2>&1 &
## Or redirect to /dev/null to discard output completely
./long-task.sh > /dev/null 2>&1 &
The 2>&1 redirects both standard output and error output. This way, the background process runs silently without cluttering your terminal.
You can check if the process is running:
ps aux | grep persistent-task.sh
And you can examine the output file:
cat persistent-output.log
The output shows the start time and iterations of the persistent task.
The disown Command
Another way to keep processes running after logout is to use the disown command. First, run a process in the background:
cd ~/project
./persistent-task.sh &
Then use disown to detach it from the terminal:
disown %1
Now this process will continue running even if you close the terminal.
These commands are essential when running tasks on remote servers where you need processes to continue after disconnecting.
Summary
In this lab, you have learned essential Linux skills for managing background processes. You have practiced:
- Creating and executing scripts in both foreground and background modes
- Running processes in the background using the
&operator - Checking the status of background processes using
jobsandps - Managing processes with
fgandbgcommands to move them between foreground and background - Terminating background processes using the
killcommand - Running processes that continue after logout using
nohupanddisown
These skills are fundamental for efficiently working in Linux environments, especially when dealing with long-running tasks or when working on remote servers. By running processes in the background, you can maximize your productivity by executing multiple tasks simultaneously without blocking your terminal session.



