0

I have a series of commands that I want to use nohup with.

Each command can take a day or two, and sometimes I get disconnected from the terminal.

What is the right way to achieve this?

Method1:

nohup command1 && command2 && command3 ...

or Method2:

nohup command1 && nohup command2 && nohup command3 ...

or Method3:

echo -e "command1\ncommand2\n..." > commands_to_run
nohup sh commands_to_run

I can see method 3 might work, but it forces me to create a temp file. If I can choose from only method 1 or 2, what is the right way?

Alby
  • 5,522
  • 7
  • 41
  • 51
  • 1
    I think (but don't know) that the answer is "either". But neither of these will let you reattach to see any output from the processes. If you want to be able to do that you want to look into something like GNU screen or tmux. – Etan Reisner Jun 12 '15 at 19:01

1 Answers1

2
nohup command1 && command2 && command3 ...

The nohup will apply only to command1. When it finishes (assuming it doesn't fail), command2 will be executed without nohup, and will be vulnerable to a hangup signal.

nohup command1 && nohup command2 && nohup command3 ...

I don't think this would work. Each of the three commands will be protected by nohup, but the shell that handles the && operators will not. If you logout before command2 begins, I don't think it will be started; likewise for command3.

echo -e "command1\ncommand2\n..." > commands_to_run
nohup sh commands_to_run

I think this should work -- but there's another way that doesn't require creating a script:

nohup sh -c 'command1 && command2 && command3'

The shell is then protected from hangup signals, and I believe the three sub-commands are as well. If I'm mistaken on that last point, you could do:

nohup sh -c 'nohup command1 && nohup command2 && nohup command3'
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631