Bash Job Control


Forking

If a command ends in &, it is pushed to the background as a child process. The shell does not await that child process returning, and can exit immediately.

These child processes are called jobs, and they are managed with three builtins:

Note that the kill, disown, and wait builtins work on both processes and jobs.


Coprocesses

With the coproc buiiltin, a child process can be instantiated in the background. Unlike a forked process, it is possible to pass data into and receive data from a coprocess.

A coprocess is started like:

coproc myproc { ps; }

An array is created as $myproc. It contains two file descriptors, the first of which can be used to read data out of the coprocess, and the second of which can be used to write data to the coprocess. As an example:

coproc myproc { ps; }
read -r output <&"${myproc[0]}"
echo "$output"

An anonymous coprocess can be created like:

coproc { ps; }

The file descriptors for this coprocess will be avialable in the shell variable, $COPROC.


Pushing to Background

With the keyboard sequence of Control+Z, a running process (such as emacs or vi) can be pushed to the background. Functionally this is the same as forking. So similarly, to resume work in the editor, use fg.


CategoryRicottone

Bash/JobControl (last edited 2023-01-29 23:07:36 by DominicRicottone)