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.
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
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.
Here’s an example showing how the next job in line takes over the default status, when the default job is removed:
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
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
Since the job is running in foreground mode, I don’t get a new command line interface prompt until the jobs finishes.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.