Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

A background process runs without any user input. The shell is not forced to
wait until the process is complete before it is freed up to allow more
commands to be input and run. When you tell a command to run in the
background, you are given its job number in brackets followed by its PID, or
process ID number. You can use this to manage the process later, if necessary.


You can input a list of several commands to run in the background. Each will
run separately and have its own PID. In this sample, a, b, and c represent
commands:


Click here to view code image
matthew@seymour:~$ a & b & c &
[1] 11427
[2] 11428
[3] 11429


You can even use pipes within background processes, and you can combine
multiples of each. The letters here represent individual commands:


Click here to view code image
matthew@seymour:~$ d & | e & f & g & | h &
[1] 11432
[2] 11433
[3] 11434


Notice that the line above becomes three separate background processes, even
though five commands were issued. That is because commands that are piped
together are treated as one process.


Moving Jobs to the Background or Foreground with


bg and fg


The shell has the concept of foreground jobs and background jobs.
Foreground jobs are process groups with control of the terminal, and
background jobs are process groups without control of the terminal.


Let’s say you start a job by running a program at the command line—maybe
something like this, which could take a while to run:


Click here to view code image
matthew@seymour:~$ find . -type f -printf "%s\t%p\n" | sort -n


When you run this, it starts in the foreground, meaning the terminal is
interactive with you only for this job, until the job is complete. This particular
job will find all files in the current directory and its subdirectories and list
them according to their size. You wouldn’t likely want to tie up your terminal

Free download pdf