Job Control

The function of starting, stopping, killing, and resuming jobs is called job control. With job control, you have full control over how processes run in your shell environment. This section describes the commands to use to view and control jobs running in your shell.

Viewing Jobs

The key command for job control is the jobs command. The jobs command allows you to view the current jobs being handled by the shell:

An output shows the current jobs being handled by the shell using the jobs command.
Description

An output shows the current jobs being handled by the shell using the jobs command.
Description

The script uses the $$ variable to display the PID that the Linux system assigns to the script; then it goes into a loop, sleeping for 10 seconds at a time for each iteration. In the example, the first script is started from the command-line interface and then paused using the Ctrl+Z key combination. Next, another job is started as a background process, using the ampersand symbol. To make life a little easier, the output of that script is redirected to a file so that it doesn’t appear on the screen.

After the two jobs were started, use the jobs command to view the jobs assigned to the shell. The jobs command shows both the stopped and the running jobs, along with their job numbers and the commands used in the jobs.

The jobs command uses a few different command line parameters, as shown in TABLE 9-3.

TABLE 9-3 The jobs command parameters.

ParameterDescription
-lList the PID of the process along with the job number.
-nList only jobs that have changed their status since the last notification from the shell.
-pList only the PIDs of the jobs.
-rList only the running jobs.
-sList only stopped jobs.

You probably noticed the plus and minus signs in the jobs command output. The job with the plus sign is considered the default job. It would be the job referenced by any job control commands if a job number isn’t specified in the command line. The job with the minus sign is the job that would become the default job when the current default job finishes processing. There will only be one job with the plus sign and one job with the minus sign at any time, no matter how many jobs are running in the shell.

The following is an example showing how the next job in line takes over the default status, when the default job is removed:

An output shows how the next job in line takes over the default status, when the default job is removed.
Description

An output shows how the next job in line takes over the default status, when the default job is removed.
Description

In this example, three separate scripts were started and then stopped. The jobs command listing shows the three processes and their status. Note that the default process (the one listed with the plus sign) is the last process started.

Then the kill command was issued to send a SIGHUP signal to the default process. In the next jobs listing, the job that previously had the minus sign is now the default job.

Restarting Stopped Jobs

You can restart any stopped job as either a background process or a foreground process. A foreground process takes over control of the terminal you’re working on, so be careful about using that feature.

To restart a job in background mode, use the bg command, along with the job number:

An output restarts a job in background mode using the b g command.
Description

An output restarts a job in background mode using the b g command.
Description

Because the job was restarted in background mode, the command-line interface prompt appears, allowing other commands to be entered. The output from the jobs command now shows that the job is indeed running (as you can tell from the output now appearing on the monitor).

To restart a job in foreground mode, use the fg command, along with the job number:

An output restarts a job in foreground mode using the f g command.
Description

Since the job is running in foreground mode, the command-line interface prompt does not appear until the job finishes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.32.222