Section 7.11. Job Control


[Page 273]

7.11. Job Control

The Korn shell supports the job control commands shown in Figure 7-20.

Figure 7-20. Korn shell job control commands.

Command

Function

jobs

Lists your jobs.

bg

Places a specified job into the background.

fg

Places a specified job into the foreground.

kill

Sends an arbitrary signal to a process or job.


The next few sections provide a description of each job control facility and examples of their use.

7.11.1. Jobs

Figure 7-21 describes the jobs shell command.

Figure 7-21. Description of the jobs shell command.

Shell Command: jobs [ -l ]

jobs displays a list of all the shell's jobs. When used with the -l option, process IDs are added to the listing. The syntax of each line of output is:

job# [ +\- ] PID Status Command

where a + means that the job was the last job to be placed into the background, and a - means that it was the second-to-last job to be placed into the background. Status may be one of the following:

  • Running

  • Stopped (suspended)

  • Terminated (killed by a signal)

  • Done (zero exit code)

  • Exit (nonzero exit code)

The only real significance of the + and - is that they may be used when specifying the job in a later command (see "Specifying a Job," below).



[Page 274]

Here's an example of jobs in action:

$ jobs                             ...no jobs right now. $ sleep 1000 &                     ...start a background job. [1]   27128 $ man ls | ul -tdumb > ls.txt &    ...another bg job. [2]   27129 $ jobs -l                          ...list current jobs. [2] + 27129  Running      man ls | ul -tdumb > ls.txt & [1] - 27128  Running      sleep 1000 & $ _ 


7.11.2. Specifying a Job

The bg, fg, and kill commands that I'm about to describe allow you to specify a job using one of several forms, as listed in Figure 7-22.

Figure 7-22. Job specifications in the Korn shell.

Form

Specifies

%integer

The job number integer.

%prefix

The job whose name starts with prefix.

%+

The job that was last referenced.

%%

Same as %+.

%-

The job that was referenced second to last.


The descriptions that follow contain examples of job specification.

7.11.3. bg

Figure 7-23 describes the bg shell command.

Figure 7-23. Description of the bg shell command.

Shell Command: bg [ %job ]

bg resumes the specified job as a background process. If no job is specified, the last-referenced job is resumed.


In the following example, I started a foreground job and then decided it would be better to run it in the background. I suspended the job using Control-Z and then resumed it in the background.


[Page 275]

$ man ksh | ul -tdumb > ksh.txt   ...start in foreground. ^Z                                ...suspend it. [1] + Stopped             man ksh | ul -tdumb > ksh.txt $ bg %1                  ...resume it in background. [1]   man ksh | ul -tdumb > ksh.txt& $ jobs                            ...list current jobs. [1] +  Running            man ksh | ul -tdumb > ksh.txt $ _ 


7.11.4. fg

Figure 7-24 describes the fg shell command.

Figure 7-24. Description of the fg shell command.

Shell Command: fg [ %job ]

fg resumes the specified job as the foreground process. If no job is specified, the last-referenced job is resumed.


In the following example, I brought a background job into the foreground using fg:

$ sleep 1000 &                       ...start a background job. [1]   27143 $ man ksh | ul -tdumb > ksh.txt &    ...start another. [2]   27144 $ jobs                               ...list the current jobs. [2] +  Running           man ksh | ul -tdumb > ksh.txt & [1] -  Running           sleep 1000 & $ fg %ma                          ...bring job to foreground. man ksh | ul -tdumb > ksh.txt     ...command is redisplayed. $ _ 


7.11.5. kill

Figure 7-25 describes the kill shell command.

Figure 7-25. Description of the kill shell command.

Shell Command: kill [-l] [ -signal] { process | %job }+

kill sends the specified signal to the specified job or processes. A process is specified by its PID number. A signal may be specified either by its number or symbolically, by removing the "SIG" prefix from its symbolic constant in "/usr/include/sys/signal.h." To obtain a list of the signal names, use the -l option. If no signal is specified, the TERM signal is sent. If the TERM or HUP signals are sent to a suspended process, it is sent the CONT signal, which causes it to resume.



[Page 276]

The following example demonstrates the use of kill:

$ man ksh | ul -tdumb > ksh.txt &   ...start a bg job. [1]   27160 $ kill -9 %1             ...kill it via a job specifier. [1] + Killed             man ksh | ul -tdumb > ksh.txt & $ man ksh | ul -tdumb > ksh.txt &   ...start another. [1]   27164 $ kill -KILL 27164          ...kill it via a process ID. [1] + Killed            man ksh | ul -tdumb > ksh.txt & $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net