Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
This question is already asked unix.stackexchange.com/questions/67006/… –  Rahul Patil Mar 7 '13 at 19:07
The question in the unix.stackexchange.com/questions/67006/… is slightly different as I wanted to maintain my order of execution . Please correct me if I am wrong in understanding the other question. –  user33767 Mar 8 '13 at 5:31
add comment

4 Answers 4

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' &
share|improve this answer
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
add comment

If you don't mind not using nohup:

{ command1 >result1 2>&1 && command2 >result2 2>&1 ; } & disown
share|improve this answer
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
add comment

Simply use :

nohup ./cmd2>result2 & nohup ./cmd1>result1 & nohup ./cmd3>result3 &
share|improve this answer
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
add comment

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

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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