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 &


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?


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' &
  • Thanks. It is working fine. Yeah, thats what I was thinking , after making the first cmd go to background, it does not affect the second command, which starts even though I wanted it to wait for the first command to finish. I need to read the manual about sending command to background.
    – user33767
    Mar 8 '13 at 5:27

If you don't mind not using nohup:

{ command1 >result1 2>&1 && command2 >result2 2>&1 ; } & disown
  • Will check that out. But currently will go with answer from Giles where the command is wrapped with sh. Thanks anyway
    – user33767
    Mar 8 '13 at 5:32
  • This is a bad idea. When user logout from the console, it will kill the session.
    – mootmoot
    Mar 8 '17 at 9:16
  • @mootmoot this works as intended, check this for yourself; commands won't receive SIGHUP when parent shell exits.
    – artyom
    Mar 10 '17 at 12:37
  • Yes, it is shell safe if you run the command using crond/crontab. But it is not pty/xterm/ssh safe. unix.stackexchange.com/questions/3886/…
    – mootmoot
    Mar 10 '17 at 12:54

Simply use :

nohup ./cmd2>result2 & nohup ./cmd1>result1 & nohup ./cmd3>result3 &
  • While running the multiple command is fine, I wanted to maintain the exact order of execution i.e. cmd2 first and then cmd1 and it is not happening with the above command. thanks
    – user33767
    Mar 8 '13 at 5:29

let us know what exactly you are doing with cmd2 and cmd1 . It depend upon type of task and output you want from your command. If you are facing issue with hitting two times enter key with command then it can be figure out by my this post

you can go by this way also

nohup `cmd2 && ccmd1` > Output.out 2> Error.err < /dev/null & 

See the below given screenshot, Here I am using 3 commands and all are executing one by one serially. I used backticks for this task. In screenshot you can also observe the date time stamp. sleep command has 60 seconds,hence after 60 seconds my next command is run successfully. I used jobs command to check is any command still running at background. Here no. of jobs you can see is 1 .


This screenshot is available in this link also,if it is not visible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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