How to Use Jobs Command in Linux: Syntax and Examples
Use the
jobs command in Linux to list all background and suspended jobs in the current shell session. It shows job IDs, status, and commands, helping you manage running tasks easily.Syntax
The basic syntax of the jobs command is simple:
jobs: Lists all current jobs with their status.jobs -l: Shows jobs with their process IDs.jobs -p: Lists only the process IDs of jobs.
This command works only in the current shell session and shows jobs started from it.
bash
jobs jobs -l jobs -p
Example
This example shows how to start a job in the background, list it with jobs, and then bring it back to the foreground.
bash
sleep 30 & jobs fg %1
Output
[1]+ Running sleep 30 &
sleep 30
Common Pitfalls
Common mistakes when using jobs include:
- Expecting
jobsto show processes started outside the current shell session. - Confusing job numbers with process IDs.
- Not using
fgorbgto manage jobs after listing them.
Remember, jobs only tracks jobs started in the current shell.
bash
sleep 30 & # Wrong: Trying to kill job by PID instead of job number kill 12345 # Right: Use job number with percent sign kill %1
Quick Reference
| Command | Description |
|---|---|
| jobs | List all current jobs with status |
| jobs -l | List jobs with process IDs |
| jobs -p | List only process IDs of jobs |
| fg % | Bring a job to the foreground |
| bg % | Resume a suspended job in the background |
| kill % | Terminate a job by its job number |
Key Takeaways
The jobs command lists background and suspended jobs in the current shell.
Use job numbers with % to manage jobs via fg, bg, or kill commands.
Jobs started outside the current shell won't appear in jobs output.
Use jobs -l to see process IDs alongside job information.
Remember to use fg or bg to control job execution after listing them.