Cheatsheet & Examples: jobs
The jobs command displays the status of jobs currently running in the background or stopped. It lists their job number, status, and the command that started the job.
Listing Active Jobs
Example Usage:
jobs
What it does: Lists all jobs that are currently running in the background, stopped, or have completed but haven't been reaped by the shell. It displays the job number, status (e.g., "Running", "Stopped", "Done"), and the command.
Command-line Arguments Explained:
- (No arguments)
Listing Jobs with Detailed Information
Example Usage:
jobs -l
What it does:
Lists all jobs, providing more detailed information than the basic jobs command. Includes process IDs (PIDs) associated with each job.
Command-line Arguments Explained:
-l: Provides a long listing format, including the process ID (PID) of the job.
Killing a Job
Example Usage:
kill %1
What it does:
Sends a signal (typically SIGTERM or SIGKILL) to the process group associated with a job. This is not directly using the jobs command, but jobs is used to find the job number. Then the kill command is used to take action on the job.
Command-line Arguments Explained:
%1: Refers to job number 1. You would replace1with the actual job number you want to target, as displayed by thejobscommand.
Bringing a Background Job to the Foreground
Example Usage:
fg %2
What it does: Brings a background or stopped job to the foreground, allowing it to take input from the terminal and be controlled by the user.
Command-line Arguments Explained:
%2: Refers to job number 2. Replace2with the actual job number you want to move to the foreground.
Stopping a Job (if running in the foreground)
Example Usage:
Ctrl+Z (applied to a foreground process)
What it does:
Sends a SIGTSTP signal to the currently running foreground process, pausing its execution and putting it into the "Stopped" state. This is not directly using the jobs command, but the stopped job would be listed by jobs.
Command-line Arguments Explained:
- (No arguments, but a specific key combination used during foreground process execution.)
Ctrl+Z: A special key combination in Unix-like systems which sends a SIGTSTP signal, stopping the currently running foreground process.
Restarting a Stopped Job in the Background
Example Usage:
bg %3
What it does: Resumes a stopped job and runs it in the background.
Command-line Arguments Explained:
%3: Refers to job number 3. Replace3with the actual job number you want to restart in the background.
Specifying a Signal to Send to a Job
Example Usage:
kill -HUP %4
What it does: Sends a specific signal to a job. This allows you to exert more granular control over the process. In this specific example, a hangup signal is sent.
Command-line Arguments Explained:
-HUP: Specifies the hangup signal to send.%4: Refers to job number 4. Replace4with the actual job number you want to target.

