18

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?

2

4 Answers 4

15

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' &
3
  • 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, 2013 at 5:27
  • If I am running the commands on a server and I already submitted one command with nohup, if I open a new terminal connection to the server and do the second command with nohup will it run both simultaneously?
    – M.O.
    Oct 6, 2022 at 15:36
  • @M.O. Yes. nohup has no effect on what can run concurrently. It only affects whether the program risks getting killed when its terminal disappears. Oct 6, 2022 at 16:29
5

If you don't mind not using nohup:

{ command1 >result1 2>&1 && command2 >result2 2>&1 ; } & disown
4
  • Will check that out. But currently will go with answer from Giles where the command is wrapped with sh. Thanks anyway
    – user33767
    Mar 8, 2013 at 5:32
  • This is a bad idea. When user logout from the console, it will kill the session.
    – mootmoot
    Mar 8, 2017 at 9:16
  • @mootmoot this works as intended, check this for yourself; commands won't receive SIGHUP when parent shell exits.
    – artyom
    Mar 10, 2017 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, 2017 at 12:54
2

Simply use :

nohup ./cmd2>result2 & nohup ./cmd1>result1 & nohup ./cmd3>result3 &
1
  • 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, 2013 at 5:29
1

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 .

nohup-multiple-command

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .