< Day Day Up > |
9.6. Job ControlJob control is a powerful feature of the C/TC shell that allows you to run programs, called jobs, in the background or foreground. Normally, a command typed at the command line runs in the foreground, and will continue until it has finished. If you have windows, job control may not be necessary, because you can simply open another window to start a new task. On the other hand, with a single terminal, job control is a very useful feature. For a list of job commands, see Table 9.2. (See "TC Shell Job Control" on page 495 for TC shell enhancements for job control.)
9.6.1 The Ampersand and Background JobsIf you expect a command to take a long time to complete, you can append the command with an ampersand and the job will execute in the background. The C/TC shell prompt returns immediately and now you can type another command. Now the two commands are running concurrently, one in the background and one in the foreground. They both send their standard output to the screen. If you place a job in the background, it is a good idea to redirect its output either to a file or pipe it to a device such as a printer. Example 9.25.1 % find . -name core -exec rm {} \; & 2 [1] 543 3 % EXPLANATION
9.6.2 The Suspend Key Sequence and Background JobsTo suspend a program, the suspend key sequence, Ctrl-Z, is issued. The job is now suspended (stopped), the shell prompt is displayed, and the program will not resume until the fg or bg commands are issued. (When using the vi editor, the ZZ command writes and saves a file. Do not confuse this with Ctrl-Z, which would suspend the vi session.) If you try to log out when a job is suspended, the message There are stopped jobs appears on the screen. 9.6.3 The jobs CommandThe C/TC shell jobs built-in command displays the programs that are currently active and either running or suspended in the background. Running means the job is executing in the background. When a job is stopped, it is suspended; it is not in execution. In both cases, the terminal is free to accept other commands. Example 9.26.(The Command Line) 1 % jobs 2 [1] + Stopped vi filex [2] - Running sleep 25 3 % jobs -l [1] + 355 Stopped vi filex [2] - 356 Running sleep 25 4 [2] Done sleep 25 EXPLANATION
9.6.4 The Foreground and Background CommandsThe fg command brings a background job into the foreground. The bg command starts a suspended job running in the background. A percent sign and the number of a job can be used as arguments to the fg and bg commands if you want to select a particular job for job control. Example 9.27.1 % jobs 2 [1] + Stopped vi filex [2] - Running cc prog.c -o prog 3 % fg %1 vi filex (vi session starts) 4 % kill %2 [2] Terminated cc prog.c -o prog 5 % sleep 15 (Press ^z) Stopped 6 % bg [1] sleep 15 & [1] Done sleep 15 EXPLANATION
|
< Day Day Up > |