30

I am using Red Hat Linux Enterprise version 5. I've noticed people sometimes running commands with a couple of & options. For example, in the below command, there are two & signs. What is the purpose of them? Are they always used together with nohup?

nohup foo.sh <script parameters> >& <log_file_name> &

6 Answers 6

15

In addition to Martin's, Ash's and Kevin's answers, sometimes you'll see an ampersand used mathematically for a bitwise AND*:

$ echo $(( 11 & 7 ))
3

In case you're not familiar with bit operators:

11: 1011
 7: 0111
-------- AND
 3: 0011

In each position there's a one bit in the first number AND the second number, set that bit to one in the answer.

*The feature in Kevin's answer is referred to as a logical AND.

To elaborate on Ash's answer, when used in redirection the ampersand can tell the shell to copy a file descriptor. In this command echo "hello" > outputfile 2>&1 the ampersand causes any output that may go to standard error (stderr, file descriptor 2) to go to the same place as standard output (stdout, file descriptor 1, the default for the left side of >). The >& outputfile operator is shorthand for > outputfile 2>&1.

Also, new in Bash 4, there are two new terminators for clauses in the case command ;& and ;;& which affect whether a case "falls through" and, if so, whether the next test is performed.

10
  • Cool, Dennis! Suppose I use ssh terminal log into a machine, then using the ssh's terminal to execute the command (suppose a long-run process), then if I quit the terminal session, then the command's long-run process will be terminated correct? And if I want the command to continue to execute even if I quit shell, should I use nohup, or & (at the end of command) or using both nohup and &?
    – George2
    Jun 15, 2010 at 13:59
  • 1
    @George2: No, >& filename outputs both stdout and stderr to the same file. If you want to redirect stderr only (and leave stdout alone) you'd do 2> filename. As for your other question, most likely you'd use both nohup and &. Jun 15, 2010 at 14:40
  • 1
    @George2: If you don't background the command then you won't get a shell prompt back so you can issue a logout or exit command. Jun 15, 2010 at 15:22
  • 2
    George: If you don’t use &, then you’ll never get your prompt back to do anything else. The terminal will be “hung” until the process (whatever it is) finishes or it’s terminated/killed. The & guarantees that the process runs in the background. However, if you logoff, the operating system will terminate all your processes, which kills your background process as well. If you use nohup you’re telling the process “ignore the command that will terminate you”. Jun 15, 2010 at 17:14
  • 1
    @George2: Yes. "immune to hangups" == "process continue" and "non-tty" == "quit terminal console session" Jun 16, 2010 at 4:27
32

In a Bash shell script, the ampersand “&” is used to fork processes:

find -name hello &

It will cause the find command to be forked and run in the background (you can always kill it by its PID).

5
  • Thanks Martin, suppose I use ssh terminal log into a machine, then using the ssh's terminal to execute the command (suppose a long-run process), then if I quit the terminal session, then the command's long-run process will be terminated correct? And if I execute the command using & in ssh terminal, even if I quit the terminal, the command's long-run process is still running, correct?
    – George2
    Jun 15, 2010 at 13:54
  • 1
    That is correct George. Background processes should continue until they exit or you kill them. However, as it has been already pointed out, you’ll need nohup to avoid the process to die when the user logs off. Jun 15, 2010 at 17:06
  • Thanks Martin! I want to know why we need to use both & and nohup, and what are their individual functions which make us achieve the goal of letting the command continue to run even if the terminal console quits. I read man page for nohup, and it is mentioned "run a command immune to hangups, with output to a non-tty", I am confused whether the immune to hangups is what you mean letting the command continue to run without impacted by quit terminal console? If so, I think using nohup is enough, and no need to use &. Any comments?
    – George2
    Jun 16, 2010 at 3:15
  • 1
    You need to use Both, if you don’t use &, the terminal will not let you enter any other commands. The best way for you to see it, is just trying it. A good example is a command that takes some time, like a find: find -name SomeName / > somefile.txt try that with nohup and & and see the differences. Jun 16, 2010 at 11:27
  • This has a great answer for the nohup question: serverfault.com/questions/311593/…
    – joshperry
    Feb 3, 2018 at 19:27
26

Why run a Linux shell command with '&'?

To get your prompt back immediately, and run the process in the background.

What are the function of them?

nohup allows the background process to continue running even after the user logs out (or exits the initiating shell).

>& redirects both standard output and standard error into the log file.

& runs the whole thing in the background, giving you your prompt back immediately.

Explanation:

Every Linux process opens three I/O channels, an input "stdin", a standard output "stdout" and a standard error output "stderr". They can be used for binary, but they are traditionally text. When most programs see stdin close, they exit (this can be changed by the programmer).

When the parent shell exits, stdin is closed on the children, and (often, usually) the children exit as well. In addition, the children receive a software signal, SIGHUP, indicating the user has "hung up" (formerly, the modem) and the default here is to exit as well. (Note, a programmer can change all of this when writing the program).

So, what nohup does is give the child process a separate I/O environment, tying up the ins and outs to something not tied to the parent shell, and shielding the child from the SIGHUP signal. Once the user disconnects, you will see the nohup background process owned by init (process 1), not the user's shell.

However nohup cannot do the job completely if the process is run in the foreground, so & is used to run the program in the background, where it can happily stay running with or without a user logged in.

4
  • Suppose I use ssh terminal log into a machine, then using the ssh's terminal to execute the command (suppose a long-run process), then if I quit the terminal session, then the command's long-run process will be terminated correct? And if I want the command to continue to execute even if I quit shell, should I use nohup, or & (at the end of command) or using both nohup and &?
    – George2
    Jun 15, 2010 at 13:52
  • 1
    Correct, use both nohup and & for the process to continue after SSH disconnects.
    – kmarsh
    Jun 15, 2010 at 15:12
  • Thanks Kmarsh! I want to know why we need to use both & and nohup, and what are their individual functions which make us achieve the goal of letting the command continue to run even if the terminal console quits. I read man page for nohup, and it is mentioned "run a command immune to hangups, with output to a non-tty", I am confused whether the immune to hangups is what you mean letting the command continue to run without impacted by quit terminal console? If so, I think using nohup is enough, and no need to use &. Any comments?
    – George2
    Jun 15, 2010 at 15:31
  • 1
    I'll edit my answer to answer.
    – kmarsh
    Jun 16, 2010 at 13:51
13

In addition to @Martin's answer: The other use of ampersand (>& as above) is to capture both stdout and stderr. Normally, if you redirected output to a file with only '>', you would only get the output to stdout, missing any errors.

3
  • 1. Thanks Ash, I want to confirm with you that ">& <log file name>", will dump all stderr and stdout to the log file, correct? 2. And using "> <log file name>" will dump all stdout to the log file, correct?
    – George2
    Jun 15, 2010 at 13:49
  • 1
    @George: Yes, that's right.
    – Ash
    Jun 15, 2010 at 23:11
  • Thanks Ash! I want to know why we need to use both & and nohup, and what are their individual functions which make us achieve the goal of letting the command continue to run even if the terminal console quits. I read man page for nohup, and it is mentioned "run a command immune to hangups, with output to a non-tty", I am confused whether the immune to hangups is what you mean letting the command continue to run without impacted by quit terminal console? If so, I think using nohup is enough, and no need to use &. Any comments?
    – George2
    Jun 16, 2010 at 3:06
4

In addition to Martin and Ash's answer, sometimes you may see the use of an && token. This is used to say "run the second command if and only if the first command ran successfully." A well-written command will, if it has any errors, not exit successfully.

[kevin@box ~]$ ls file && echo removing file && rm file
ls: file: No such file or directory
[kevin@box ~]$ touch file
[kevin@box ~]$ ls file && echo removing file && rm file
file
removing file
[kevin@box ~]$
2
  • Cool, Kevin! Suppose I use ssh terminal log into a machine, then using the ssh's terminal to execute the command (suppose a long-run process), then if I quit the terminal session, then the command's long-run process will be terminated correct? And if I want the command to continue to execute even if I quit shell, should I use nohup, or & (at the end of command) or using both nohup and &?
    – George2
    Jun 15, 2010 at 13:54
  • Either one would work. NOHUP was the original way of doing it, I imagine(but am totally guessing), but the background works now. An important difference is when running an interactive shell by sshing into a remote system. The NOHUP process would run in the foreground, but would keep running if you exit, whereas the background process would return immediately after you spawned the command. But when you try to exit an interactive shell with a command running in the background, you are warned first.
    – Kevin M
    Jun 17, 2010 at 3:03
2

Martin's answer is good, but a bit ambiguous. The command is always fork'ed and exec'ed, but normal shell behavior is to wait on the command until it exits. The ampersand puts it into the background so that you get your terminal prompt back and you can do other things. If the process spits out data to stdout or stderr, this will be intermixed with whatever you're doing at the prompt, and may confuse you. This is why you redirect then with >& /path/to/logfile.txt.

In response to George2, normal behavior for a shell exiting is to send the SIGHUP signal to all processes in the same process group (essentially stuff you spawned) and they usually will terminate. If you want this to continue even if you close the shell process, you can use the nohup command to make them ignore this signal and keep running. There's a special name for this type of process, it's called a daemon process (pronounced 'demon').

3
  • Thanks Rich! Suppose I use ssh terminal log into a machine, then using the ssh's terminal to execute the command (suppose a long-run process), then if I quit the terminal session, then the command's long-run process will be terminated correct? And if I want the command to continue to execute even if I quit shell, should I use nohup, or & (at the end of command) or using both nohup and &? And why?
    – George2
    Jun 15, 2010 at 14:52
  • Hey george, sorry i'm responding so late. Yes, if you quit the shell, the long-run process will be terminated. If you want it to keep going you need to do both nohup (to not have it terminated when the shell closes) and & (to put in background, so you can Crtl-D or exit your shell). You can also look at the setsid command, which will allow your process to run, having it ignore SIGHUP by a slightly different way. Jul 30, 2010 at 15:31
  • Great answer, explains best practices for background-running shell-started processes. Oct 9, 2012 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.