Skip to content

jobs Command: Managing Background Processes

Summary

The jobs command provides a convenient way to list, monitor, and manage background processes in your current shell environment. It is useful for checking the status of processes you've sent to the background using & or Ctrl+Z followed by bg.

Introduction

The jobs command is a built-in shell command that displays a list of background jobs, their process IDs (PIDs), and their current status. This command is invaluable for users who frequently run long-running tasks in the background and need to keep track of their progress or manage them. It allows you to bring background processes to the foreground, stop them, or kill them.

Use Case and Examples

Listing all background jobs

jobs
This simple command lists all currently running or stopped background jobs associated with your current shell session.

Listing jobs with more detail

jobs -l
This command provides a more detailed output, including the process ID (PID) of each job. This is helpful for using other commands like kill.

Bringing a specific job to the foreground

fg %1
This command brings job number 1 to the foreground. The % symbol indicates that you're referring to a job number. Replace 1 with the actual job number you want to bring to the foreground.

Stopping a specific job

kill -STOP %1
This command sends the STOP signal to job number 1, effectively pausing its execution. You can then resume it later with bg %1 or bring it to the foreground with fg %1.

Killing a specific job

kill %1
This command sends the TERM signal to job number 1, requesting it to terminate. If the job doesn't terminate gracefully, you can use kill -9 %1 (sends SIGKILL) as a last resort, but be cautious as this forcefully terminates the process without allowing it to clean up.

Commonly used flags

Flag Description Example
-l Lists process IDs (PIDs) in addition to the normal output. jobs -l
-n Lists only jobs that have changed status since the last notification. jobs -n
-p Lists process IDs only, one PID per line. jobs -p
-r Lists only running jobs. jobs -r
-s Lists only stopped jobs. jobs -s


Share on Share on

Comments