The Ampersand (&) is:
and the shell:
- does not wait for the command to finish,
- returns an return status of 0 (true, successful).
The format for running commands in the background is:
command1 & [command2 & ...]
- With bash script
How does it work ?
- By adding the ampersand (&) after a command, you start the application in background.
- When you use the command prompt to run the appropriate .sh files, by adding “&” you start them as a background task.
Where is the output redirected?
The process inherits stdout/stderr from the shell (so it still writes to the terminal).
- The process in principle also inherits stdin, but as soon as it tries to read from stdin, it is halted.
- It is put into the list of background jobs the shell manages, which means especially:
- It is listed with jobs and can be accessed using %n (where n is the job number).
- It can be turned into a foreground job using fg, in which case it continues as if you would not have used & on it (and if it was stopped due to trying to read from standard input, it now can proceed to read from the terminal).
If the shell received a SIGHUP, it also sends a SIGHUP to the process. Depending on the shell and possibly on options set for the shell, when terminating the shell it will also send a SIGHUP to the process.