Shell – Running multiple nohup commands in background

background-processnohupprocessshell

Got two commands cmd1 and cmd2. Of the two, cmd2 takes longer to finish. Need to run cmd2 and then cmd1.

Tried to run them in following way:

bash$ (nohup ./cmd2>result2 &) && nohup ./cmd1>result1 &

or

bash$ (nohup ./cmd2>result2 &) ; nohup ./cmd1>result1 &

But both time I can see cmd1 is not waiting for cmd2 to finish and result1 gets filled.

How to make cmd1 run after cmd2 when both should be nohup process and run in background?

Best Answer

You made both cmd1 and cmd2 run in parallel. You said: “Start cmd2 in the background and sever any association with it. Start cmd1 in the background and sever any association with it.” You meant: “Start cmd2 in the background; when it's complete, start cmd1 (also in the background).” Since there is no longer any association with the background task, you need to devise a background task that performs cmd2 then cmd1. This is cmd2; cmd1 (or rather cmd2 && cmd1 to run cmd1 only if cmd2 succeeds), and you'll need to tell nohup to start a shell running in the background for that.

nohup sh -c './cmd2 >result2 && ./cmd1 >result1' &
Related Question