|
Job ControlThe shell provides facilities for controlling jobs. A job is any command sequence. For example:
When a command is started in the background (that is, with &), the shell prints out the job number inside brackets ([]) as well as the process number:
When a job finishes, the shell prints the message
where n is the job number of the finished job, and sequence is the text of the command sequence used to create the job. The jobs command may be used to print the status of jobs that haven't yet finished.
The + and - after the job number mark the current and previous jobs, respectively. The current job is the last job sent to the background, and the previous job is the next-to-the-last job sent to the background. Several built-in commands may be given a job number or the current or previous job as arguments. The shell's built-in kill command can be used to terminate a job running in the background. The argument to it can be a process number or a percent sign (%) followed by a job number, a + (current job), a � (previous job), or another % (also current job).
The preceding kill could have used %+ or %% to refer to the same job. The first few characters of the command sequence can also be used to refer to a job; for example, kill %pic would have worked in the preceding example. Stopped Jobs and the fg and bg CommandsIf you are running a job in the foreground (without an &) and you want to suspend it, you can press the Ctrl+z key. The job stops executing, and the shell prints the message
The stopped job is made the current job. To have it continue executing, you must use the fg or bg command. The fg command with no arguments causes the current job to resume execution in the foreground, and bg causes the current job to resume execution in the background. You can also use a job number, the first few characters of the pipeline, a +, a -, or a % preceded by a to specify any job to the fg and bg commands. These commands print out the command sequence to remind you what is being brought to the foreground or sent to the background.
The preceding sequence is one of the most often used with job control: sending a job mistakenly started in the foreground to the background. If a job running in the background tries to read from the terminal, it is stopped, and the message
is printed. It can then be brought to the foreground with the fg command. After entering input to the job, it can be stopped (with the Ctrl+z) and returned to the background until it again requests input from the terminal. Output from a background job normally goes directly to the terminal. The command
causes any background job that attempts to write to the terminal to be stopped and the message
to be printed. (Note that Bash generates slightly different messages than the ones shown here.) The following shows how job control might be used:
|
|
No comments:
Post a Comment