Bash - Ampersand (&) - (Asynchronous|Parallel) control operator
Table of Contents
About
If a command is terminated by the control operator ampersand &, the shell executes the command in the background in a subshell.
This is known as executing the command in the background. The shell does not wait for the command to finish, and the return status is 0 (true).
When job control is not active (see Job Control), the standard input for asynchronous commands, in the absence of any explicit redirections, is redirected from /dev/null.
By adding the ampersand (&) to the end of 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.
Articles Related
Format
The format for running commands in the background is:
command1 & [command2 & ...]
Example
startdemo.sh&
Process
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.