Shell Jobs
A shell job is a command that runs in the background while you keep typing. Jobs let one shell do slow work and quick work at the same time.
Start a job
The & sends sleep to the background. jobs lists it, and kill %1 stops it.
sleep 60 &
jobs
kill %1
echo "Job stopped."Bring a job back
fg brings a job to the front. bg lets a stopped job carry on in the background.
fg %1
bg %1Job numbers
Each job has a number in square brackets. Use % with the number to name it.
sleep 60 &
sleep 60 &
jobs
kill %1
kill %2
echo "Both jobs stopped."Example
sleep 60 &
echo "Working while the job runs."
jobs
kill %1
echo "Job stopped."Key points
- Job: a command that runs in the background while the shell stays free.
- Use & at the end of a command to start it as a job.
- jobs: a command that lists the background jobs of the current shell.
- Use fg to bring a job back to the front.
- Use bg to let a stopped job carry on in the background.
- Job number: the number in square brackets that names a job, as in %1.
In this example, sleep starts as a background job. The shell stays free, so echo runs at once. jobs shows the running job, and kill %1 stops it.
Note
A job belongs to the shell that started it. Close the shell and the job ends.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does jobs show?