Trapping Background Process

I am calling one shell script from my wrapper shell script
The pseudo code for the same is

  nohup  sh script1.sh
  script.sh is  calling  script2.sh inside it  5 times followed by a  wait command 
  
  
  #1 /bin/bash
  nohup sh script2.sh 1 &
  nohup sh script2.sh 2 &
  nohup sh script2.sh 3 &
  nohup sh script2.sh 4 &
  nohup sh script2.sh 5 &
  wait.
  connect sql --> stmp success/fail status to error log table.

The sql statement at the bottom of the script should insert a success flag if all the background scriptts are successful.
If one or more background script fails then it should stamp the error message in the errorlog table with the error code and PID.

Can you guys focus some light on the above !!!
Many Thanks

Does it work? Why do you want to break it?

Note that the line in red is just a comment. To have a line like that specify the shell to be used to run the script, you need #! instead of #1 and the #! must appear at the start of the line (with no leading spaces), and there shouldn't be any spaces between #! and the pathname of the shell you want to use.

For the rest of it, I'll let you form the sql, but the following will show you how to get save the PIDs and the exit status of each of the background jobs:

#!/bin/bash
nohup sh script2.sh 1 &
pid1=$!
nohup sh script2.sh 2 &
pid2=$!
nohup sh script2.sh 3 &
pid3=$!
nohup sh script2.sh 4 &
pid4=$!
nohup sh script2.sh 5 &
pid5=$!
wait $pid1
ec1=$?
echo "$pid1 exited with exit code $ec1"
wait $pid2
ec2=$?
echo "$pid2 exited with exit code $ec2"
wait $pid3
ec3=$?
echo "$pid3 exited with exit code $ec3"
wait $pid4
ec4=$?
echo "$pid4 exited with exit code $ec4"
wait $pid5
ec5=$?
echo "$pid5 exited with exit code $ec5"

Thanks Don ! It helped . I have mentioned the above code as pseudo code hence didn't give much emphasize on the syntax like you have mentioned about the bang.