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.

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 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

# 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

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.

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

Practical Examples

# Run a command in the background
./myscript.sh &

# List all jobs to see their numbers
jobs
# Output might show: [1] Running ./myscript.sh &

# Bring the background job to the foreground
fg %1

# After pausing with Ctrl-Z, bring the most recent job back
fg

# If you have multiple jobs, bring a specific one forward
fg %2

The bg Command

Think of bg as telling a paused process "keep going, but don't take up my screen." The bg command takes a stopped (paused) process and tells it to continue running in the background. This is particularly useful when you started a process in the foreground, realized it would take a while, and want to keep it running while you do other things.

A background process still executes and may still show some output in your terminal, but it doesn't prevent you from typing other commands. It's like having music playing while you work on something else.

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
./myscript.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
jobs
# Output should show: [1]+ Running ./myscript.sh &

Starting Background Processes

Think of the & symbol as a "set it and forget it" instruction. When you add & to the end of a command, you're telling Linux to run that command in the background from the start. This is useful for commands that take a long time but don't need your constant attention.

Practical Examples

# Create a simple script that runs for a while
echo 'echo "I am running"; while sleep 15; do echo "I am running"; done' > myscript.sh

# Make it executable
chmod +x myscript.sh

# Run it in the background from the start
./myscript.sh &
# You'll see something like: [1] 12345
# The number 12345 is the process ID

# The script runs in the background, and you can continue using the terminal
ls -la

# Check what's running in the background
jobs

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

Working with Multiple Jobs: A Complete Example

Multitasking Workflow

# Create a few test scripts for demonstration
echo 'echo "Script 1 started"; while sleep 10; do echo "Script 1 running..."; done' > script1.sh
echo 'echo "Script 2 started"; while sleep 5; do echo "Script 2 running..."; done' > script2.sh
echo 'echo "Script 3 started"; while sleep 15; do echo "Script 3 running..."; done' > script3.sh

# Make them executable
chmod +x script*.sh

# Start script 1 in the foreground
./script1.sh
# Press Ctrl-Z to pause it
# Output: [1]+ Stopped ./script1.sh

# Start script 2 in the background
./script2.sh &
# Output: [2] 12346

# Start script 3 in the foreground
./script3.sh
# Press Ctrl-Z to pause it
# Output: [3]+ Stopped ./script3.sh

# List all jobs
jobs
# Output:
# [1]  Stopped  ./script1.sh
# [2]  Running  ./script2.sh &
# [3]+ Stopped  ./script3.sh

# Resume script 1 in the background
bg %1
# Output: [1]+ ./script1.sh &

# Check jobs again
jobs
# Output:
# [1]  Running  ./script1.sh &
# [2]  Running  ./script2.sh &
# [3]+ Stopped  ./script3.sh

# Bring script 3 to the foreground
fg %3
# Now script 3 has control of the terminal

# Press Ctrl-C to terminate script 3
# Bring script 1 to the foreground
fg %1

# Now script 1 has control of the terminal
# Press Ctrl-C to terminate it

# Check remaining jobs
jobs
# Only script 2 should still be running