0
0
Bash-scriptingHow-ToBeginner · 3 min read

How to Use jobs Command in Bash: Syntax and Examples

The jobs command in bash shows the status of background jobs started in the current shell session. Use jobs alone to list jobs, or with options like -l for detailed info. It helps track and manage processes running in the background.
📐

Syntax

The basic syntax of the jobs command is simple:

  • jobs: Lists all current background jobs with their status.
  • jobs -l: Shows jobs with their process IDs (PIDs).
  • jobs -p: Lists only the PIDs of the jobs.

This command works only for jobs started in the current shell session.

bash
jobs [-l] [-p]
💻

Example

This example shows how to start a background job and then use jobs to see it:

bash
sleep 30 &
jobs
jobs -l
jobs -p
Output
[1]+ Running sleep 30 & [1]+ 12345 12345
⚠️

Common Pitfalls

Common mistakes when using jobs include:

  • Trying to see jobs from other shell sessions (jobs only shows jobs in the current shell).
  • Not starting jobs with & to run them in the background.
  • Confusing jobs with system-wide process commands like ps.

Always start a command with & to run it in the background before using jobs.

bash
sleep 30
jobs
# No jobs shown because sleep runs in foreground

sleep 30 &
jobs
# Shows the background job
Output
No output for first jobs command (no background jobs) [1]+ Running sleep 30 &
📊

Quick Reference

OptionDescription
(no option)List all background jobs with status
-lList jobs with process IDs
-pList only process IDs of jobs

Key Takeaways

Use jobs to see background jobs started in the current shell.
Start commands with & to run them in the background before using jobs.
Use jobs -l to see process IDs for better job management.
jobs only shows jobs from the current shell session, not system-wide processes.