Saturday, December 19, 2009

Job Control








 

 










Job Control



The shell provides facilities for controlling jobs. A job is any command sequence. For example:





who | wc



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:





$ who | wc &

[1] 832

$



When a job finishes, the shell prints the message





[n] + sequence



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.





$ jobs

[3] + Running make ksh &

[2] � Running monitor &

[1] Running pic chapt2 | troff > aps.out &



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).





$ pic chapt1 | troff > aps.out &

[1] 886

$ jobs

[1] + Running pic chapt1 | troff > aps.out &

$ kill %1

[1] Done pic chapt1 | troff > aps.out &

$



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 Commands



If 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





[n] + Stopped (SIGTSTP) sequence



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.





$ troff memo | photo

Ctrl+z

[1] + Stopped (SIGTSTP) troff memo | photo

$ bg

[1] troff memo | photo &

$



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





[n] - Stopped (SIGTTIN) sequence



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





stty tostop



causes any background job that attempts to write to the terminal to be stopped and the message





[n] - Stopped (SIGTTOU) sequence



to be printed. (Note that Bash generates slightly different messages than the ones shown here.)



The following shows how job control might be used:





$ stty tostop

$ rundb Start up data base program

??? find green red Find green and red objects

Ctrl+z This may take a while

[1] + Stopped rundb

$ bg So put it in the background

[1] rundb &

... Do some other stuff

$ jobs

[1] + Stopped(tty output) rundb &

$ fg Bring back to foreground

rundb

1973 Ford Mustang red

1975 Chevy Monte Carlo green

1976 Ford Granada green

1980 Buick Century green

1983 Chevy Cavalier red

??? find blue Find blue objects

Ctrl+z Stop it again

[1] + Stopped rundb

$ bg Back to the background

[1] rundb &

... Keep working until it's ready












     

     


    No comments: