2

I want to start a command, wait until it finishes, and then start a series of commands following it, and I would like to chain these together.

I have tried testing with variations of commands using the ; and && operators, but my desired behavior has been expressed.

What I'd like is to run the following:

command1 && command2 && command3; (command4; command5; command6;)

Given the above command, I would want the following to occur:

Run command1
When command1 has successfully completed, start command2
When command2 has successfully completed, start command3
When command3 has succuessfully completed start (command4, command5 and command6) in parallel
Veridian
  • 3,531
  • 12
  • 46
  • 80
  • Possible duplicate of [Bash: run one command after another, even if I suspend the first one (Ctrl-z)](http://stackoverflow.com/questions/13600319/bash-run-one-command-after-another-even-if-i-suspend-the-first-one-ctrl-z) – KeithC May 03 '17 at 21:26

1 Answers1

1

This code would achieve what you want:

command1 && command2 && command3; nohup command4 & nohup command5 & nohup command6 &

Note: using nohup on the terminal (not on a script) can hold the prompt and create a file nohup.out somewhere if you don't use a redirection such as > /dev/null 2>&1 when invoking it.

Using nohup would guarantee that the processes invoked with nohup would continue running even if you close the terminal where you run the command (basically, nohup runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out).

This simpler code version could also work:

command1 && command2 && command3; command4 & command5 & command6 &

However, if you use the simpler version above and you (for example) close the terminal before (say) command 5 was completed, that command would be interrupted and not finish.

Jamil Said
  • 2,033
  • 3
  • 15
  • 18
  • 2
    Did you test this? I expect you to get a syntax error. Try `nohup command4 & nohup command5 & nohup command 6` (eg, omit the semi-colons. `&` replaces the semi-colon, it does not precede it.) – William Pursell May 03 '17 at 21:49
  • You're correct, syntax error. I posted before testing from the top of my head and was testing now and getting the error. I fixed the code, now it works as it should. – Jamil Said May 03 '17 at 21:51
  • 1
    Interesting about the `&` not needing a `;` learned something :) I've only run them on their own lines, never would have noticed – Xen2050 May 04 '17 at 04:29