8

Effectively what I'm trying to accomplish is the following, where cmd1 takes a while to run (~1 minute) and cmd2 returns almost instantly whenever run:

  1. cmd1 starts
  2. wait a little while (~10 seconds)
  3. cmd2 runs
  4. wait a little while again
  5. cmd2 runs again
  6. some time passes
  7. cmd1 finishes

It is essential this be scripted since it has got to go inside a rather large loop, which runs cmd1 with many different parameters, and then does the same all over again for several other longish-running commands.

How can I accomplish this?

Mala
  • 7,466

1 Answers1

14
cmd1 &
cmd1_pid=$!
sleep 10
cmd2
sleep 10
cmd2
wait $cmd1_pid

explanation: cmd1 & launches a process in the background of the shell. the $! variable contains the pid of that background process. the shell keeps processing the other cmds. sleep 10 means 'wait a little while'. OP just wants to fire cmd2 in linear order so that part is trivial. at the end of the script snippet we just wait for cmd1 to finish (it might be even finished earlier) with wait $cmd1_pid.

akira
  • 63,447