Job Control
Created: 01/25/2007
General Information
Sometimes processes can take time to complete, thus tying up your console session. What if you want to run multiple processes in the background on the same shell session? With the built-in job control, you can. There are three main conditions a job can be in: Running, Stopped/Suspended, or Terminated. To control them, we'll use thefg and bg commands.Requirements
Usage
If there is a process that you know will take a while to complete and would like it to be forked to the background immediately, you can append a& at the end of the command to achieve this. For this example, we'll just create a simple timer.|
$ sleep 100 & [1] 45862 $ |
[1] is the job number which is used by other job control commands to reference it. The second number 45862 is the process ID that the system assigns. So, the job number is unique to the shell and the process ID is unique to the operating system. If you forget what job number you want to reference, you can always see a list of all the jobs in the job controller:|
$ jobs [1]+ Running sleep 100 & $ |
fg command:|
$ fg 1 sleep 100 |
|
$ fg 1 sleep 100 ^Z [1]+ Stopped sleep 100 $ |
bg command:|
$ bg 1 [1]+ sleep 100 & $ |
|
$ kill %1 [1]+ Terminated: 15 sleep 100 $ |
Note: When jobs are running in the background, they may still send their output to the terminal. To disable the output, send it to > /dev/null:
|
$ portsnap fetch update > /dev/null & |
Author: Jon LaBass
jon at bsdguides dot org