Section 6.14. Job Control


[Page 239 (continued)]

6.14. Job Control

Job control allows you to suspend and resume the execution of a process begun from the Bash command line. Typing a Control-Z while a process is running will suspend it. You can then use the bg or fg shell commands to resume it in the background or foreground, respectively, as described in Figures 6-48 and 6-49.


[Page 240]

To see all jobs associated with the current shell, use the jobs built-in (Figure 6-46).

Figure 6-46. Description of the jobs shell command

Shell command: jobs [-lrs]

jobs displays a list of all the shell's jobs. When jobs is used with the -l option, process IDs are included in the listing. If the -r option is used, list only currently running jobs. If the -s option is used, list only currently stopped jobs.


6.14.1. Specifying a Job

Each of the job control commands (bg, fg, and kill) requires that a job be specified upon which to act. Figure 6-47 shows the forms available to specify a Bash job.

Figure 6-47. Job specifications in Bash.

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.

%name

Refers to a process whose name begins with name.

%?name

Refers to a process where name appears anywhere in the command line.


The specifier must uniquely identify a job. If more than one job matches the specifier, Bash reports an error.

6.14.2. bg

Figure 6-48 describes the bg shell command.

Figure 6-48. 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.



[Page 241]

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.

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


6.14.3. fg

Figure 6-49 describes the fg shell command.

Figure 6-49. 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 bash | ul -tdumb > bash.txt &   ...start another. [2]  27144 $ jobs                                ...list the current jobs. [2] +  Running           man bash | ul -tdumb > bash.txt & [1] -  Running           sleep 1000 & $ fg %ma                            ...bring job to foreground. man bash | ul -tdumb > bash.txt     ...command is redisplayed. $ _ 


6.14.4. kill

Use the kill built-in to send a signal to a job (Figure 6-50).


[Page 242]

Figure 6-50. Description of the kill shell command.

Shell command: kill [-s signame ] [-n signum] jobspec or pid

kill sends the specified signal to the specified process. Either jobspec (e.g., "%1") or a process ID is required. If the -s option is used, signame is a valid signal name (e.g., SIGINT). If the -n option is used, signum is the signal number. If neither -s nor -n is used, a SIGTERM signal is sent to the process.





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