Managing Jobs in the Korn Shell

The Korn shell enables you to run jobs in the background, which frees your terminal to perform other commands. You can also run jobs in the foreground. You can use Korn shell commands to list current jobs as well as to stop a job.

A job is a process that the shell can manage. Shells start and control jobs. Because jobs are processes, each job has an associated PID. The shell also assigns each job a sequential job ID number.

The shell enables you to run multiple jobs at the same time. Job control commands enable you to manage multiple jobs within a shell. There are three types of jobs that shells manage: foreground jobs, background jobs, and stopped jobs.

When you perform a command in a terminal window, the command occupies that terminal window until it completes. This type of job is called a foreground job.

When you enter an ampersand (&) symbol at the end of a command line, the command runs without occupying the terminal window. The shell prompt is displayed immediately after you press Return. This type of job is called a background job.

If you press Control-Z for a foreground job, or perform the stop command for a background job, the job stops. This job is called a stopped job.

Job control commands enable you to place jobs in the foreground or background, and to start or stop jobs. The table describes the commands you can use for job control.

Job Control Commands
Command Value
jobs
Lists all jobs that are currently running or are stopped in the background
bg %n
Runs the current or specified job in the background (n is the job ID)
fg %n
Brings the current or specified job into the foreground (n is the job ID)
Control-Z
Stops the foreground job and places it in the background as a stopped job
stop %n
Stops a job that is running in the background (n is the job ID)


Note: You can control a job by using these commands only in the shell in which the job started.

Running a Job in the Background

To run a job in the background, enter the command you want to run along with an ampersand (&) symbol at the end of the command line. For example, you can run the sleep command in the background as follows:

$ sleep 500 &
[1]     3028
$
Note: The sleep command suspends execution of a program for n seconds.

The shell returns the job ID number it assigned to the command, contained in brackets, and the PID associated with the command. You use the job ID number to manage the job with job control commands. The kernel can use the PID number to manage the job.

When a background job has finished and you press Return, the shell displays a message indicating that the job is done.

[1] + Done
sleep 500 &
$

Listing Current Jobs

 You can use the jobs command to display the list of jobs that are running or stopped in the background. For example:$ jobs
[1] + Running
sleep 500 &
$

Bringing a Background Job Into the Foreground

 You can use the fg command to bring a background job to the foreground. For example:


$ fg %1
sleep 500

Note: The foreground job occupies the shell until the job is completed, stopped, or stopped and placed into the background.


Sending a Foreground Job to the Background

You can use the Control-Z keys and bg command to return a job to the background. The Control-Z keys suspend the job, and place it in the background as a stopped job. The bg command runs the job in the background. For example:

$ sleep 500
^Z[1] + Stopped (SIGTSTP)        sleep 500
$ jobs
[1] + Stopped (SIGTSTP)        sleep 500
$ bg %1
[1]     sleep 500&
$ jobs
[1] +  Running                 sleep 500
$

Note: When you place a stopped job in either the foreground or the background the job restarts.

command1 $(command2)

To ignore the special meaning of the dollar sign metacharacter, use the following command:

$ echo '$SHELL'
$SHELL


Leave a Comment

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top