Whenever you start a program at the shell, it's assigned a job number. Jobs are quite separate from processes and are designed primarily for users to understand what programs are running on the system.
You can see which jobs are running at any one time by typing the following at the shell prompt:
When you run a program, it usually takes over the shell in some way and stops you from doing anything until it's finished what it's doing. However, it doesn't have to be this way. Adding an ampersand symbol (&) after the command will cause it to run in the background. This is not much use for commands that require user input, such as vi or top, but it can be very handy for commands that churn away until they're completed.
For example, consider the updatedb command, which updates the database used for the locate command (see Chapter 15). Once run, this command could take many minutes to complete, during which time the shell will effectively be unusable. However, you can type the following to retain use of the shell:
When you do this, you'll see the following:
This tells you that updatedb is running in the background and has been given job number 1. It also has been given process number 7483 (although bear in mind that when some programs start, they instantly kick off other processes and terminate the one they're currently running, so this won't necessarily be accurate).
If you've ever tried to run a GUI program from the shell, you might have realized that when it's running, the shell is inaccessible. Once you quit the GUI program, the control of the shell will be returned to you. By specifying that the program should run in the background with the & (ampersand symbol), you can run the GUI program and still be able to type away and run other commands.
You can send several jobs to the background, and each one will be given a different job number. In this case, when you wish to switch into a running job, you can type its number. For example, the following command will switch you to the background job assigned the number 3:
You can exit a job that is currently running by pressing Ctrl+Z. It will still be there in the background, but it won't be running (officially, it's said to be sleeping). To restart it, you can switch back to it, as just described. Alternatively, you can restart it but still keep it in the background. For example, to restart job 2 in the background, leaving the shell prompt free for you to enter other commands, type the following:
You can bring the command in the background into the foreground by typing the following:
When a background job has finished, something like the following will appear at the shell:
+ Done updatedb
Using jobs within the shell can be a good way of managing your workload. For example, you can move programs into the background temporarily while you get on with something else. If you're editing a file in vi, you can press Ctrl+Z to stop the program. It will remain in the background, and you'll be returned to the shell, where you can type other commands. You can then resume vi later on by typing fg or typing % followed by its job number.