A job is a running process in memory. Job control is a shell feature that moves jobs from the foreground to background, suspends running jobs, and restarts suspended jobs. You can also put a job into a wait state, where it waits for another job to finish and then restarts. The shell provides a mechanism to carry out these tasks with the help of some intrinsic shell commands. The process of job control is explained next .
Whenever a user starts a program or command, in UNIX terminology a job is initiated. To start another job, you have to wait for the job to finish and get the shell command prompt again. All these jobs are foreground jobs, as the user can interact with the running program. The user can't issue another command until the job is finished and the command prompt is back. This simply means that when a job is running in the foreground, no other job can be started. If you want to start more than one job simultaneously , you need to start them in the background. A background job is one that gives you back the shell command prompt after it starts running. For example, a big number- crunching program may need hours to finish and you just don't want to wait for it to finish. Instead, you would like do some other tasks. Background jobs are run by the shell but at the same time the shell lets you submit other jobs.
To start a job in the background you can put the & symbol at the end of your command prompt. Usually, the jobs that don't need any interactive input are started in the background. You may also run programs in the background that need input by redirecting stdin to some file on the disk. If a background job sends its output to stdout , it is displayed on your terminal screen unless you have redirected it. Redirection and pipes can be used with background jobs. Next is a typical output of a job started in the background.
$ ll /usr >mylist & [1] 11633 $
Once a job is started in the background, the shell prints the job ID and process ID (PID) numbers of the command that started the job on the terminal display. You can then use this job ID to bring a job into the foreground at any time. You can also list background-running jobs with the jobs command.
$ jobs [1] + Stopped vi myfile [2] - Running vi file2 $
The jobs command lists all background jobs, job IDs, and whether these jobs are running or stopped. A plus symbol ( + ) in the output of this command shows that this is the current job; a minus sign ( - ) shows which job is scheduled to be run next after the current job. A -l switch with the jobs command shows the PID of all jobs.
Jobs may be in a running or stopped state. A job may be stopped if it needs input that is not available or is suspended by the user. When a job is completed, it goes into the Done state. If you run a job in the background and it tries to read something from stdin, it is automatically stopped by the shell until brought into the foreground by the user.
Many times you start a job in the foreground and then you want to do something else without abandoning the program running in the foreground. For example, you may have started the vi editor and you need to copy files without abandoning the editor. The POSIX shell provides a mechanism to suspend a current job temporarily. To suspend a running job, you can use the key sequence represented by the susp value in the stty -a output, often defined as . Pressing that key sequence suspends the current job and gives you the command prompt. The job is suspended, and if you use the jobs command, you will see the job is stopped. You will also see a plus ( + ) symbol showing that this is the current job.
$ jobs [1] + Stopped vi myfile [2] - Stopped vi file2 $
To find the value of the susp sequence, use the stty -a command. If this value is not set, you can use the stty command to set its value as follows .
$ stty susp ^z $
The ( ^ ) symbol shows the control key. After this command, you can use the key sequence to suspend a job.
All suspended jobs can be resumed with the foreground ( fg ) command. The same command is used to bring background jobs to the foreground. To bring a job to the foreground, you need to know the job id with the jobs command. If you don't specify any job ID, the current job is brought into the foreground. For example, when you suspend the vi editor to do some other work, the fg command will bring the vi screen back, and you can use it again in the normal way.
$ jobs [1] + Stopped vi myfile [2] - Stopped vi file2 $ fg %2
Job numbers are used with the percent ( % ) symbol with the fg command. You can also use the command name with the fg command, which is sometimes more convenient when writing shell scripts.
Study BreakJob Control
|
As soon as you suspend a foreground job, it goes into a stopped state. You can start the job, keeping it in background with the bg (background) command. To send a foreground job into the background, first suspend it and then use the bg command. If there is more than one suspended job, you need to provide a job ID to the bg command to bring a particular job into running state from stopped state. The following sequence of commands lists background jobs and then changes the state of job number 2 to running.
$ jobs [1] + Stopped vi myfile [2] - Stopped vi file2 $ bg %2 [1] + Stopped vi myfile [2] - Running vi file2 $
There is no direct way to stop a running job. We can, however, adopt an alternate method to stop a running job. We can bring a background job into the foreground and then suspend it. You may need to stop a running job temporarily when it is taking a lot of system resources and you want to run a more important job.
At any point, if you want to wait for background jobs to be finished, just use the wait command. This command stops the command prompt until all background jobs are finished. If you want to stop the wait command, just press the key on your keyboard. We can also wait for a particular job to finish by specifying the job number as an argument to the wait command.
$ wait %2
Top |