series of commands using nohup

462 views Asked by At

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?

1

There are 1 answers

0
Keith Thompson On BEST ANSWER
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'