Multi-process tasks come about because processes may create additional child processes, and a single shell command may consist of a pipeline of multiple communicating processes. For example, a command to select lines containing the text "title", sort these alphabetically, and display the result in a pager.
This creates at least three processes: one for grep , one for sort , and one for less. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination usually , the entire group of processes gets suspended. Jobs are managed by the operating system as a single process group , and the job is the shell's internal representation of such a group.
A job can be referred to by a handle [lower-alpha 2] called the job control job ID or simply job ID , which is used by shell builtins to refer to the job. Job control and job IDs are typically only used in interactive use, where they simplify referring to process groups; in scripting PGIDs are used instead, as they are more precise and robust, and indeed job control is disabled by default in bash scripts.
The POSIX standard specifies two commands for resuming suspended jobs in the background and foreground, respectively bg and fg. These were modeled after the Korn shell job control commands. Typically, the shell keeps a list of jobs in a job table. Recall that a job corresponds to a process group, which consists of all the members of a pipeline and their descendants. The jobs command will list the background jobs existing in the job table, along with their job number and job state stopped or running.
When a session ends when the user logs out exits the shell, which terminates the session leader process , the shell process sends SIGHUP to all jobs, and waits for the process groups to end before terminating itself. The disown command can be used to remove jobs from the job table, so that when the session ends the child process groups are not sent SIGHUP, nor does the shell wait for them to terminate. They thus become orphan processes , and may be terminated by the operating system, though more often this is used so the processes are adopted by init the kernel sets their parent process to init and continue executing as daemons.
Alternatives to prevent jobs from being terminated include nohup and using a terminal multiplexer. A job running in the foreground can be stopped by typing the suspend character Ctrl-Z. A job running in the foreground can be interrupted by typing the interruption character Ctrl-C.
A stopped job can be resumed as a background job with the bg builtin , or as the foreground job with fg. These signals stop the process by default, but they may also be handled in other ways.
Shells often override the default stop action of SIGTTOU so that background processes deliver their output to the controlling terminal by default. Anonymous Not logged in Create account Log in. Hand W iki.
From HandWiki. Namespaces Page Discussion. The shell prompt is displayed immediately after you press Return. This is an example of a background job. 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 job control commands. For example, run the sleep command in the background. With the job ID, you can use the job control commands to manage the job whereas the kernel uses PIDs to manage jobs. When a background job is complete and you press Return, the shell displays a message indicating the job is done. You can use the jobs command to list the jobs that are currently running or suspended in the background.
The bg command runs the job in the background. For example: 1. Skip to primary navigation Skip to main content Skip to primary sidebar Skip to footer navigation.
0コメント