Previous Table of Contents Next


Module 9
bg (csh, ksh)

DESCRIPTION

The internal bg command resumes stopped jobs by placing them in the background. The bg command is part of the job control feature of the C shell and the Korn shell. Without arguments, bg refers to the current job. The current job is the last job you stopped orsuspended.

COMMAND FORMAT

Following is the general format of the bg command.

     bg [ %job ... ]

Arguments

The following arguments may be passed to the bg command.

%job A job number associated with a process. If the interactive option is on, job may be one of the following:
%num A job number for an associated process.
%string The job whose name begins with string.
%?string The job whose name contains string.
%+
%% Current job; the last process you stopped or suspended.
%- Previous job; the process before the current job.

RELATED COMMANDS

Refer to the fg, jobs, kill, and suspend commands described in modules 51, 68, 70, and 129.

APPLICATIONS

The bg command is used to restart suspended jobs in the background. You can suspend a job running in the background or one running in the foreground. This is handy if you are running a command and you need to perform some other function. For example, let's assume you are running a long process in background. You realize you need to create a file the process requires. You can suspend the job, create the file, then restart the process by using the bg command.

TYPICAL OPERATION

In this activity you use the bg command to restart a process in the background. Begin at the shell prompt. Before you start you may wish to check your suspend character, type stty -a or stty everything (BSD) and see to what character the suspend (susp) character is set.

1.  Type cd / and press Return.
2.  Type du -s > /dev/null and press Return. This produces a disk usage list and writes the output in the UNIX trash can device.
3.  Press Ctrl-Z to suspend the du job.
     cj> du -s > /dev/null
     ^Z
     [1] + Stopped               du -s > /dev/null
4.  Now type jobs and press Return to display all background and suspended jobs.
     cj> jobs
     [1] + Stopped               du -s > /dev/null
5.  Type ps and press Return to display the time used by the du process. If you want, wait 15 or 20 seconds and repeat this step. Notice the time used by du has not increased. This is because the job is suspended.
6.  Type bg %1 to start the du job running in background. Your screen should resemble the following,
     cj> bg %1
     [1]du -s > /dev/null

The format of the job display from bg may vary from system to system.
7.  Type ps and press Return to display the time used by the du process. Notice that the time used has increased since the last time you performed a ps. This is because the job is running in the background.
8.  Terminate the background job by typing kill %1 and pressing Return. The following message is displayed,
     cj> kill %1
     [1] + Terminated           du -s > /dev/null
9.  Turn to Module 51 to continue the learning sequence.
Previous Table of Contents Next