Hello! Welcome to the next lesson in our module on Process, Service, and Task Management.
In our previous lesson, we explored how to influence the scheduler's decisions by adjusting process priority with nice and renice. We learned how to make important tasks run faster and background tasks consume fewer resources.
Today, we'll build on that by learning how to control where a process runs—in the foreground, where it monopolizes your terminal, or in the background, where it runs independently. This lesson addresses the learning outcome: Use background and foreground job control in the shell. Mastering this will allow you to multitask efficiently from a single command-line session, a fundamental skill for any Linux administrator.
1. Foreground vs. Background Processes
When you type a command and press Enter, it typically runs in the foreground. This means the process takes control of your terminal. You can't type new commands until the current one finishes. A simple example is sleep 30; your terminal will be unresponsive for 30 seconds.
A background process, on the other hand, runs without taking control of your terminal. You can start a long-running task in the background and immediately get your command prompt back to continue working.
The shell provides tools to manage these processes, which it tracks as jobs. A job is essentially a shell's internal wrapper for a process or a group of related processes (like a command pipeline).
Linux Back to Basics Episode 8: Bash Job Management
To start, let's watch a brief clip from the video "Linux Back to Basics Episode 8: Bash Job Management" by Douglas Rumbaugh. This will clarify the distinction between a system-level process and a shell-level job.
Watch from 00:24 to 02:50. Focus on understanding that a process is any executing program, while a job is how your specific shell session keeps track of the processes it has started.
2. Managing Foreground Jobs: Suspending and Terminating
You already know one way to interact with a foreground job: terminating it.
Ctrl+C: Sends theSIGINT(interrupt) signal, which usually tells the process to terminate.
But what if you don't want to kill the process, just get your shell back temporarily? For that, you can suspend it.
Ctrl+Z: Sends theSIGTSTP(terminal stop) signal, which pauses the process and returns control of the terminal to you.
Linux Back to Basics Episode 8: Bash Job Management
Let's see a demonstration of a foreground process being suspended with Ctrl+Z.
Watch the segment from 03:35 to 05:16. Notice how Ctrl+C kills the process, but Ctrl+Z just stops it, creating a job that the shell can manage.
When you suspend a job, it is paused and is not consuming CPU cycles, but it still exists in memory.
3. Juggling Jobs: jobs, bg, and fg
Once you have a stopped job (or several), you need a way to manage them.
Listing Jobs with jobs
The jobs command lists all the jobs associated with your current shell session, showing their job number and status (e.g., Running or Stopped).
jobs
The output might look like this:
[1]+ Stopped sleep 100
[2]- Stopped vim my_script.sh
The + indicates the most recently used job, and - indicates the second most recent. These are the defaults for commands like fg and bg.
Running a Job in the Background with bg
A suspended job is not doing anything. To make it resume execution in the background, you use the bg (background) command.
bg %1 # Resumes job 1 in the background
If you run bg without a job number, it will resume the most recent job (the one marked with +).
Starting a Job Directly in the Background
The most common way to start a background job is to append an ampersand (&) to the end of the command.
find / -name "*.log" > log_files.txt &
[1] 23451
The shell immediately returns a job number ([1]) and the Process ID (23451), and you get your prompt back while find does its work.
Bringing a Job to the Foreground with fg
If you need to interact with a background job again, you can bring it to the foreground with the fg (foreground) command.
fg %1 # Brings job 1 to the foreground
Running fg by itself will bring the most recent job to the foreground.
Let's watch a series of clips that demonstrates these core commands.
Linux Back to Basics Episode 8: Bash Job Management
This sequence from the "Linux Back to Basics" video will walk you through using jobs, bg, fg, and the & operator.
Watch the following four short segments to see these commands in action: Listing jobs: 05:16 - 05:58 Using bg: 05:58 - 06:44 Using fg: 06:44 - 07:17 Using &: 14:53 - 16:00
This image provides a great visual summary of the workflow:

Test your understanding!
You need to run a script data_processing.sh that takes about 10 minutes, but you also need to immediately check a log file. What are two different ways you could accomplish this in the same terminal?
Show answer
Method 1: Start it directly in the background.
Append & to the command.
./data_processing.sh &
# Now you can immediately check the log file
less /var/log/syslog
Method 2: Start it, suspend it, and move it to the background.
- Run the script:
./data_processing.sh - Press
Ctrl+Zto suspend it. - Run
bgto let it continue in the background. - Now you are free to check the log file.
./data_processing.sh
^Z
[1]+ Stopped ./data_processing.sh
bg
[1]+ ./data_processing.sh &
# Now you can immediately check the log file
less /var/log/syslog
Method 1 is more direct if you know ahead of time that you want the job in the background.
4. Terminating Background Jobs
To terminate a background job, you use the kill command, but instead of a PID, you can give it a job specification (or "jobspec"). The most common jobspec is the job number prefixed with a percent sign (%).
kill %1
It is crucial to use the % symbol. If you typed kill 1, the system would try to kill process ID 1, which is the init system—a very different and protected process!
Linux Back to Basics Episode 8: Bash Job Management
This next clip clearly explains the importance of using the percent sign (%) when killing a job and demonstrates the process.
Watch from 09:04 to 13:31. Pay close attention to the "permission denied" error when trying kill 1 and how kill %1 succeeds.
Beyond %n, there are other useful jobspecs.
BashGuide/JobControl - Job Specifications
The "BashGuide/JobControl" page on Greg's Wiki provides a concise but detailed list of job specifications. This will broaden your understanding of how to refer to jobs.
Read the section titled 'Job Specifications'. Focus on understanding these key jobspecs: %n, %% (or %+), and %-.
%n: Job numbern.%%or%+: The most recent job.%-: The second most recent job.
So, fg is equivalent to fg %%, and kill %- would kill the second-to-last job you backgrounded or suspended.
5. Keeping Jobs Alive: nohup and disown
What happens to your background jobs if you close the terminal or your SSH connection is lost? The shell sends a SIGHUP (hangup) signal to all its jobs, causing them to terminate. To prevent this, you can use nohup or disown.
-
nohup: Use this when you start a command. It makes the process immune to theSIGHUPsignal. Any output that would normally go to the terminal is redirected to a file namednohup.out.nohup long_running_command.sh & -
disown: Use this for a job that is already running. It removes the job from the shell's job list, so the shell will no longer send it aSIGHUPsignal.# Suppose you have job %1 running disown %1
Linux Back to Basics Episode 8: Bash Job Management
This final clip explains the 'hangup' problem and demonstrates how to use both nohup and disown to ensure your processes keep running even after you log out.
Watch from 16:00 to 22:08. Observe how a process started with nohup continues to run after the terminal is closed. Then see how disown achieves the same for an existing job.
For further reading on all the topics we've covered, the DigitalOcean article "How To Use Bash's Job Control" (LINK) serves as an excellent written reference.
Conclusion
You've just learned a powerful set of tools for managing processes interactively within a single shell session. This allows you to run multiple tasks, switch between them, and ensure long-running jobs are not accidentally terminated.
Key Takeaways:
- Foreground vs. Background: Foreground jobs block your shell; background jobs do not.
- Key Shortcuts:
Ctrl+Cterminates a foreground job, whileCtrl+Zsuspends it. - Core Commands:
command &: Starts a command in the background.jobs: Lists all jobs in the current session.bg [%job]: Resumes a suspended job in the background.fg [%job]: Brings a background or suspended job to the foreground.kill %job: Terminates a job using its jobspec.
- Persistence:
nohupanddisownare used to prevent jobs from being terminated when the shell session ends.
Preview of the Next Lesson:
So far, we have focused on managing individual processes and jobs that you start yourself. In our next lesson, we will move to a higher level of process management: inspecting service status and logs using systemctl. This will introduce you to the world of system services—the daemons that run persistently in the background to provide core system functionality.