jobs, fg & bg commands in Linux


Jobs Command:

jobs: Jobs command is used to list the jobs that you are running in the background and in the foreground. If the prompt is returned with no information no jobs are present. All shells are not capable of running this command. This command is only available in the csh, bash, tcsh, and ksh shells.

Options:

-p           Display the PID of process group leader

-l             Provides more information about each job listed. This information includes the job                 number,  current job, process group ID, state, and the command that initiated the job.
-n            Display only jobs that have stopped or exited since last notified

-r             Display running jobs

-s            Display stopped jobs

 Each job will be listed with a number; to resume a job, enter %  (percent sign) followed by the number of the job. For example, To restart job number two
$ %2




jobs fg bg in Linux


Output of jobs command:


$ jobs
[1]-   Stopped                 mail
[2]    Running                 fi.sh &
[3]+  Stopped                 sleep 200


First field (job-number): 

Indicates the process group number to use with the wait, fg, bg, and kill commands. When used with these commands, prefix the job number with a % (percent sign).

Second Field (Current Field):

A + (plus sign) tells it is the default jobs for the fg or bg commands. This job ID can also be specified using the %+ (percent sign, plus) or %% (double percent sign).

A - (minus sign) tells that the job becomes the default if the current default job exits. This job ID can also be specified using %- (percent sign, minus).
For other jobs, the current field is a space character. Only one job can be identified with a +, and only one job can be identified with a -.



Third Field (State of job): 

It can be running, stopped, done etc


bg Command: 


bg : Runs jobs in the background. bg command resumes suspended jobs in the current environment by running them as background jobs. If the specified job is already running in the background, the bg command has no effect and exits successfully. If no JobID parameter is supplied, the bg command uses the most recently suspended job as below.

$ bg

If more jobs are running we can use job no to send specific job in background as below.

$ bg 2

A job can be suspended using ctrl+z key sequence. That job can be restarted in the background using the bg command.

Example:

Start a job in foreground

$ sleep 120     

Press Ctrl+z to stop the current job.

Now move the last stopped job to background.

$ bg

If the output of the jobs command displays the following stopped job:

[2] + Stopped     sleep 120 &

use the job number to resume the sleep 120 & job by entering:

$ bg %2

The screen displays the revised status of job 2:

[2] sleep 120 &

A job can also be run in background by adding & at end of the command

Example:

$ sleep 120 &


fg Command:


fg : Runs jobs in the foreground.  the fg command moves a background job in the current environment into the foreground. Use the job ID parameter to indicate a specific job to be run in the foreground. If this parameter is not supplied, the fg command uses the job most recently suspended, placed in the background, or run as a background job as below.

$ fg

Specifying the job id will resume that particular job as below. 

$ fg 2



Linux Processes                                                batch and at commands in Linux                         

No comments: