Job Control - Shell Scripting

Earlier, you saw how to use the Ctrl-Z key combination to stop a job running in the shell. After you stop a job, the Linux system lets you either kill or restart it. You can kill the process by using the kill command. Restarting a stopped process requires sending it a SIGCONT signal.

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:

$ cat test4
#!/bin/bash
# testing job control
echo "This is a test program $$"
count=1
while [ $count -le 10 ]
do
echo "Loop #$count"
sleep 10
count=$[ $count + 1 ]
done
echo "This is the end of the test program"
$ ./test4
This is a test program 29011
Loop #1
[1]+ Stopped ./test4
$ ./test4 test4out
[2] 28861
$
$ jobs
[1]+ Stopped ./test4
[2]- Running ./test4 test4out &
$

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, I start the first script from the command line interface, then stop it using the Ctrl-Z key combination. Next, I start another job as a background process, using the ampersand symbol.To make life a little easier, I redirected the output of that script to a file so that it wouldn’t appear on the monitor.

After the two jobs were started, I used 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, shown in Table below.

The jobs Command Parameters

You probably noticed the plus and minus signs in the 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 wasn’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 jobs Command Parameters
Here’s an example showing how the next job in line takes over the default status, when the default job is removed:

$ ./test4
This is a test program 29075
Loop #1
[1]+ Stopped ./test4
$ ./test4
This is a test program 29090
Loop #1
[2]+ Stopped ./test4
$ ./test4
This is a test program 29105
Loop #1
[3]+ Stopped ./test4
$ jobs -l
[1] 29075 Stopped ./test4
[2]- 29090 Stopped ./test4
[3]+ 29105 Stopped ./test4
$ kill -9 29105
$ jobs -l
[1]- 29075 Stopped ./test4
[2]+ 29090 Stopped ./test4
$

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

I then used the kill command 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

Under bash job control, 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:

$ bg 2
[2]+ ./test4 &
Loop #2
$ Loop #3
Loop #4
$ jobs
[1]+ Stopped ./test4
[2]- Running ./test4 &
$ Loop #6
Loop #7
Loop #8
Loop #9
Loop #10
This is the end of the test program
[2]- Done ./test4
$

Since I restarted the job in background mode, the command line interface prompt appears, allowing me to continue with other commands. 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:

$ jobs
[1]+ Stopped ./test4
$ fg 1
./test4
Loop #2
Loop #3

Since the job is running in foreground mode, I don’t get a new command line interface prompt until the jobs finishes.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics