I'm curious to know if it makes a difference where the '&' operator is used in code when a process has input/output redirection to run a process in the background
What are the differences/are there any differences between these lines of code in terms of running the process in the background. If there are, how can I determine what the differences are going to be?
setsid python script.py < /dev/zero &> log.txt &
setsid python script.py < /dev/zero & > log.txt &
setsid python script.py < /dev/zero > log.txt &
setsid python script.py & < /dev/zero > log.txt
There are two uses of &
here. One is as a so-called control operator. Every command is terminated by a control operator such as &
, ;
or <newline>
. The difference between them is that ;
and <newline>
run the command in the foreground and &
does it in the background.
setsid python script.py < /dev/zero & > log.txt &
setsid python script.py & < /dev/zero > log.txt
These two lines, therefore, actually execute two commands each. The first is equivalent to the two commands:
setsid python script.py < /dev/zero &
> log.txt &
And the second is equivalent to:
setsid python script.py &
< /dev/zero > log.txt
If you're wondering, yes, > log.txt
and < /dev/zero > log.txt
are both legal commands. Lacking a command name, they simply process the redirections: each one creates an empty file called log.txt
.
setsid python script.py < /dev/zero &> log.txt &
This version with &>
is different from the one with & >
. &>
without a space is a special redirection operator in bash that redirects both stdout and stderr.
setsid python script.py < /dev/zero > log.txt &
This final version is similar to the previous one except it only redirects stdout to log.txt
. stderr continues to go to the terminal.