Unix jobs Command - Usage Guide
๐ง What Is a "Job" in Unix?
A job refers to a command or process started from a shell. Jobs can be:
- Foreground: Active and attached to the terminal.
- Background: Running behind the scenes.
- Stopped: Suspended with
Ctrl+Z.
๐ Usage of jobs Command
jobs
This displays all jobs started from the current shell, including their ID, state, and command.
๐ Example Output
$ sleep 100 &
[1] 12345
$ jobs
[1]+ Running sleep 100 &
⌨️ Related Job Control Commands
| Command | Description |
|---|---|
& | Run a command in the background |
Ctrl+Z | Pause (suspend) a foreground job |
fg | Resume job in foreground |
bg | Resume job in background |
kill %1 | Kill job with ID 1 |
jobs -l | Show job PID |
jobs -p | Show job all processes |
✅ When It's Useful
- Managing long-running processes during an active shell session
- Suspending/resuming tasks without killing them
- Debugging shell-based automation or accidental background tasks
๐งช Step-by-Step Example: Managing Jobs
๐น Step 1: Start a Long-Running Command
$ sleep 300
๐น Step 2: Suspend with Ctrl + Z
^Z
[1]+ Stopped sleep 300
๐น Step 3: Check Jobs
$ jobs
[1]+ Stopped sleep 300
๐น Step 4: Resume in Background
$ bg %1
[1]+ sleep 300 &
๐น Step 5: Bring Back to Foreground
$ fg %1
sleep 300
๐น Step 6: Kill the Job
$ jobs
[1]+ Running sleep 300 &
$ kill %1
$ jobs
[1]+ Terminated sleep 300
✅ Summary
&to run in backgroundCtrl+Zto pausebg/fgto resumekillto terminate
No comments:
Post a Comment