Background Jobs
A job is a command that the shell manages. A background job runs while the prompt stays free.
Start a job in the background
The & starts a sleep job in the background. jobs shows it as Running. kill %1 stops job number 1.
$ sleep 300 &
$ jobs
$ kill %1
$ jobs
$ jobsBring a job to the front
fg brings the job to the front. The prompt returns when the sleep ends.
$ sleep 2 &
$ fgExample
$ cd ~/practice
$ sleep 300 &
$ jobs
$ kill %1
$ jobs
$ sleep 2 &
$ fgKey points
- Use & at the end of a command to start it in the background.
- The shell prints a job number and a PID when a job starts.
- Use jobs to list the jobs that are running.
- The foreground is the front, where a command holds the prompt.
- Use fg to bring a job to the foreground.
- kill %1 stops job number 1.
- A stopped job shows once as Terminated, then it clears.
- A background job keeps running while you type new commands.
In this example, the first command moves into the practice folder. A sleep job starts in the background. jobs shows it as Running. kill %1 stops it. jobs names it once as Terminated. A short sleep job starts next. fg brings it to the front. The prompt returns after two seconds.
Note
Always stop a sleep job in the same block that starts it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does & do at the end of a command?