CIS120 Linux Fundamentals by Scott Shaper

jobs, fg and bg Commands

Think of Linux processes like juggling multiple balls at once. The jobs, fg, and bg commands are like your hands - they help you control which ball gets your attention right now (foreground), which ones keep moving on their own (background), and which ones pause in mid-air (suspended). These commands let you multitask in your terminal, working on several things simultaneously without needing to open multiple terminal windows. They work the same on Ubuntu Server as on other Linux systems.

The examples below use a practice script created by the course setup.sh script. After running setup, you'll have ~/playground/chapter6/count_loop.sh—a script that every 5 seconds increments a counter and appends a line to a file (count_output.txt by default). You can run it in the foreground (your terminal is busy until you press Ctrl-Z or Ctrl-C) or in the background (it keeps running while you run other commands, and you can watch the file grow with tail -f count_output.txt). Run cd ~/playground/chapter6 before trying the examples.

NOTE: In this lesson we will be running the script using the ./ in front of the script name to run it. We could also run the script using the bash command. For example: bash ~/playground/chapter6/count_loop.sh. However, we will be using the ./ syntax for the sake of consistency.

Quick Reference

Command What It Does Common Use
jobs Lists all background and stopped processes Checking what processes are running
fg Brings a background process to the foreground Resuming work on a paused process
bg Continues a stopped process in the background Running a process without occupying your terminal
command & Starts a command in the background Running a command while keeping terminal access
Ctrl-Z Pauses the current foreground process Temporarily stopping a process to do something else
Ctrl-C Terminates the current foreground process Stopping a process that you don't want to continue

When to Use These Commands

Understanding Process States

Think of a Linux process as being in one of three states, like a traffic light:

Process State What It Means Real-World Comparison
Foreground Process is actively running and connected to your terminal Green light - moving and has your full attention
Background Process is running but doesn't have terminal control Yellow light - moving but in the background
Stopped Process is paused and waiting to be resumed Red light - completely paused and not moving

The bg Command

When you first run a script it will run in the foreground by default. This means that your terminal will be "busy" and you won't be able to run other commands until the script finishes. So if we start this script ./count_loop.sh you will see the cursor blinking as the script runs. However you will not be able to run any other commands because the script is runnning in the foreground. To solve this issue we need to pause the script and run it in the background.

To pause the script we can press Ctrl-Z. You will see the following output:

[1]+ Stopped ./count_loop.sh

This means that the process has been paused.

To resume the script we can run the bg command. If you have multiple paused processes you can use the %n syntax to resume a specific process. For example: bg %1. You will see the following output:

[1]+ ./count_loop.sh &

This means that the process has been resumed and is now running in the background and we can now run other commands in the terminal.

NOTE: You can also run the script in the background when you first start it by adding the & symbol to the end of the command. For example: ./count_loop.sh &. You will see the following output:

[1] 12345

This is the process ID of the background process. You can use this process ID to refer to the process in other commands.

This is the same as running the bg command after the script has been paused. However, if you run the script in the background when you first start it, you will not need to pause it and then resume it. You can just run the bg command to resume it.

Syntax What It Does When to Use
bg Continues the most recent stopped job in the background When you've just paused a job with Ctrl-Z and want it to continue running
bg %n Continues job number n in the background When you want to resume a specific paused job

Practical Examples

# Start a long-running process
./count_loop.sh

# Press Ctrl-Z to pause it
# You'll see something like: [1]+ Stopped ./myscript.sh

# Continue the paused process in the background
bg

# If you have multiple stopped jobs, resume a specific one
bg %2

# Check that your job is now running in the background (we will cover jobs command later in this lesson)
jobs
# Output should show: [1]+ Running ./myscript.sh &

The fg Command

Think of fg as bringing a task to center stage. When you have processes running in the background or temporarily paused, the fg command pulls them back into the foreground, making them the active process in your terminal.

When a process is in the foreground, it has control of your terminal - you'll see its output, and your keyboard input goes to that process. This is useful when you need to interact with a program or monitor its output carefully. That is not the case with our script as it does not give any output to the terminal. To bring it to the foreground all we need to do is run the fg command. Doing so will bring the script back to the foreground and the cursor will start blinking again and we will not be able to run other commands in the terminal until the script finishes, is paused again or is terminated.

Here is an example of how to bring the script to the foreground:

fg

You will see the following output:

[1]+ ./count_loop.sh

This means that the process has been brought back to the foreground and we cannot run other commands in the terminal until the script finishes, is paused again or is terminated.

When a script is running in the foreground and you press Ctrl-C it will terminate the script. You will see the following output:

[1]+ Terminated ./count_loop.sh

This means that the process has been terminated.

If you have multiple paused processes you can use the %n syntax to bring a specific process to the foreground. For example: fg %1. You will see the following output:

[1]+ ./count_loop.sh

This means that the process has been brought back to the foreground and we can now run other commands in the terminal.

Syntax What It Does When to Use
fg Brings the most recent job to the foreground When you want to continue your most recent background task
fg %n Brings job number n to the foreground When you want to continue a specific background task
fg %- Brings the previous job to the foreground When you want to switch between two jobs

 

The jobs Command

Think of jobs as your process manager - it shows you a list of all the tasks you've set aside or put on autopilot in your current terminal session. It doesn't show all processes on your system, just the ones you've started in your current shell that are either running in the background or are temporarily paused.

Each job in the list gets a job number (like %1, %2) that you can use to refer to it with other commands. The jobs command also tells you the status of each job - whether it's Running (in the background) or Stopped (paused).

Option What It Does When to Use
-l Lists jobs with their PIDs (Process IDs) When you need the numeric ID of a process
-p Shows only PIDs of the jobs When you only need process IDs for another command
-r Lists only running jobs When you want to see only active background jobs
-s Lists only stopped jobs When you want to see only paused jobs

Practical Examples

To see the example in action you first need to run the count_loop.sh script. You can do this by running the following command in your terminal, we will start by running it in the background.

cd ~/playground/chapter6
./count_loop.sh &

You should see the following output:

[1] 12345

This is the process ID of the background process. You can use this process ID to refer to the process in other commands.

Then you can run the following commands to see what the jobs command does:

# List all jobs in your current terminal session
jobs

# See all jobs with their process IDs
jobs -l

# Check which jobs are currently running in the background
jobs -r

# See which jobs are currently stopped/paused
jobs -s

Practice with the course script (count_loop.sh)

After running setup.sh, go to ~/playground/chapter6. The script count_loop.sh writes a counter to a file every 5 seconds. Try both ways:

cd ~/playground/chapter6

# Run in the background from the start — you get your prompt back immediately
./count_loop.sh &
# You'll see something like: [1] 12345

# In the same terminal, watch the output file grow (new line every 5 seconds)
tail -f count_output.txt
# Press Ctrl-C to stop watching (the script keeps running in the background)

# Check your jobs
jobs
# Bring the job to the foreground when you want to stop it
fg
# Then press Ctrl-C to stop the script

Foreground vs background: feel the difference

cd ~/playground/chapter6

# First, run in the foreground — your terminal is "busy" and you can't run other commands
./count_loop.sh
# Wait a few seconds; you'll see nothing in the terminal (output goes to the file)
# Press Ctrl-Z to pause it. You'll see: [1]+ Stopped ./count_loop.sh

# Now continue it in the background so you can use the terminal
bg
# Output: [1]+ ./count_loop.sh &

# Run other commands while the counter keeps updating the file
ls -l count_output.txt
tail -3 count_output.txt
jobs

# When done, bring the job to the foreground and stop it with Ctrl-C
fg
# Press Ctrl-C

Keyboard Shortcuts

Keyboard shortcuts are like quick gestures that let you control processes without typing full commands. They're essential for efficient terminal work.

Shortcut What It Does When to Use
Ctrl-C Sends the SIGINT signal to terminate the foreground process When you want to stop a program immediately
Ctrl-Z Sends the SIGTSTP signal to pause the foreground process When you want to temporarily pause a program
Ctrl-D Sends EOF (End of File) to the foreground process When you want to signal the end of input to a program

Practical Examples

# Start a script in the foreground
./myscript.sh

# Press Ctrl-Z to pause it
# Output: [1]+ Stopped ./myscript.sh

# Resume it in the background
bg

# Bring it back to the foreground
fg

# Press Ctrl-C to terminate it completely
# The script will stop running

Tips for Success

Common Mistakes to Avoid

Best Practices