= 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 [[Bash/BuiltinCommands|builtins]]: * `bg`, which moves a job to the background * `fg`, which resumes a job in the foreground * `jobs`, which lists running jobs Note that the `kill`, `disown`, and `wait` builtins work on both processes and jobs. ---- == Coprocesses == With the `coproc` [[Bash/BuiltinCommands#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 [[Bash/Array|array]] is created as `$myproc`. It contains two [[Bash/FileDescriptor|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 [[Bash/ShellVariables#Job_Control|shell variable]], `$COPROC`. ---- == Pushing to Background == With the keyboard sequence of Control+Z, a running process (such as [[Emacs|emacs]] or [[Vim|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