Bash – In a shell script, how can I (1) start a command in the background (2) wait x seconds (3) run a second command while that command is running

background-processbashshell-script

This is what I need to happen:

  1. start process A in the background
  2. wait for x seconds
  3. start process B in the foreground

How can I make the wait happen?

I'm seeing that 'sleep' seems to halt everything and I don't actually want to 'wait' for process A to finish entirely. I've seen some time based loops but I'm wondering if there's something cleaner.

Best Answer

Unless I'm misunderstanding your question, it can simply be achieved with this short script:

#!/bin/bash

process_a &
sleep x
process_b

(and add an extra wait at the end if you want your script to wait for process_a to finish before exiting).

You can even do this as an one-liner, without the need for a script (as suggested by @BaardKopperud):

process_a & sleep x ; process_b
Related Question