4

I see a lot of people are using the following command:

nohup ./sendData.sh >logs/send.log 2>&1 &

Since the nohup has already been used, do I still need to add & on the trail?

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
Jack
  • 5,540
  • 13
  • 65
  • 113
  • 1
    Yes, otherwise it will not run in the background and you can only use your shell again once the process is finished. – pfnuesel Nov 11 '13 at 18:25
  • I mean do I need to use them together? Can I just use nohup only or & only? Thanks! – Jack Nov 11 '13 at 18:26
  • 1
    why don't you try it with a simpler command, i.e. `nohup sleep 60 &` and fiddle around with it until you see what each element adds to the picture. Good luck. – shellter Nov 11 '13 at 18:31
  • Note that there is an arguably "cleaner" alternative: http://stackoverflow.com/a/9646339/572743 – Damon Nov 11 '13 at 19:00

1 Answers1

18

tells the terminal to not hang up, even when the parent terminal is closed. & tells the terminal to send the process to the background. So

command &

will send the command to the background. You can then use the shell to invoke other commands. If the terminal is exited, the jobs are killed.

nohup command

will not send the command to the background, but when you close the terminal, the process will live on until it is finished or killed.

nohup command &

will send the command to the background and the process will not be killed even when you close the terminal. Usually when using , one also wants the process to be in the background, that's why you see the combination of and & more often than not.

pfnuesel
  • 14,093
  • 14
  • 58
  • 71
  • 2
    Excuse me for being a nitpick on your otherwise excellent reply. `nohup` doesn't tell the terminal anything, that's more like what e.g. `disown -h` would do. Nohup is a program that ignores the `HUP` signal and that fork-executes its first commandline parameter, forwarding the others. So, technically, the terminal does try to hang up, it's just that nobobdy cares what it has to say. – Damon Nov 11 '13 at 18:54