"Threading" in Shell Scripts

I have a situation where I need to:

  1. Do some preamble setup in Script A
  2. Start Scripts B, C, D and E together, so they run concurrently
  3. When all four scripts have exited, do some cleanup in Script A

At the moment I have run flags in scripts B - D which set whether or not the script is still running, and after firing off the four scripts, script A sits in a wait loop checking these flag files.

Is there a better way to do this?

The background is this is a backup script for an AIX server using snapshots - script A takes the JFS2 snapshots on the file systems we wish to backup (they all need to be done at the same time, as we have related data distributed across different filesystems - this cannot be changed), scripts B - D do the actual backups of these filesystems from the snapshots (three rsyncs to a different server, and one tape backup), and then script A removes the snapshots once all the file copies (including to tape) are completed.


Solution 1:

With bash, you can do this:

init # do the preamble setup of script A

scriptB & # start in background
scriptC & # start in background
scriptD & # start in background

wait # wait for all background jobs to finish

cleanup # do the cleanup part of script A