Discover bash jobs

Bash allows you to execute process in the background, the famous &. In interactive mode, every command executed with & at the end will be executed in the background. When the command exits, the following message is reported:

$ sleep 1 &
[1] 8269
$
[1]+  Done                    sleep 1
$

This means that the command sleep 1 exited with return code 0 (success). Actually, if the command would return anything else, the following message would be reported:

$ (sleep 1 && exit 2) &
[1] 8666
$ 
[1]+  Exit 2               ( sleep 1 && exit 2 )
Input/Output

When a process is started in the background, /dev/stdout and /dev/stderr, standard output and standard errors, are inherited from parent process. Here is an example which shows the stdout inheritage:

$ ( echo Hello world ) &
[1] 9414
Hello world

Concerning standard input, if a command needs to read from it, bash will suspend the command until you put the command to foreground (fg). This will permit the command to read from /dev/stdin:

$ ( read p ) &
[1] 9434
$ 
[1]+  Stopped                 ( read p )
$ fg
( read p )
Hello world
$
Send a command to background after started in the foreground

With bash, it is possible to send a command to background afterwards. First, you must send the STOP signal with the key sequence CTRL + Z. This will stop the command and will give back control, ready to execute another command. To put the command in the background, use bash command bg (background):

$ sleep 100
^Z
[1]+  Stopped                 sleep 100
$ bg
[1]+ sleep 100 &
$  jobs -l
[1]+ 27482 Running   sleep 100 &

Please note that is also possible to go back to the first command at any time with fg (foreground).

List running jobs

Command jobs allows to show the list of running jobs:

$ jobs -l -p
Kill a child process

Bash can send signals (especially SIGTERM and SIGKILL) by using job shortcuts %1 to %n. %1 means first background job. By using the internal kill command, it is possible to kill child process:

$ sleep 100 &
[1] 28013
$ kill %1
$ 
[1]+  Terminated              sleep 100
$

You know another hints

Post them in the comments bellow!