How to run Background process one after another

Hii Friends,
I am using Perl CGI. I am running A SCP Command via Perl CGI in Background. Like


    system("scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/  &)

This Copy Process takes some times lets say 15 min.
Now I want When This copy process gets complete then send me an email.
But My problem is This command is running in Background So How can i send a mail.
I know how to send Mail but i don't know how to send mail when process is running in Background.
Please help me out?

Create a script with:

#!/bin/sh
scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/
mailx ...

Then execute that script from Perl in background:

system("/path/to/your/script.sh &")

Here would be the Algo:

  1. Run the SCP command in background
  2. Store the PID of the SCP Job (in Shell, PID of immediate backgrounded job can be derived by: BGPROC_PID=$!
  3. Monitor the processes in a loop for the presence of above Backgrounded process

Here is equivalent UNIX Code

 
scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/  1>/dev/null 2>/dev/null &
BGPID=$!
 
while [ 1 ]; do
    PR_EX=$(ps -ef | awk -v BGPID=${BGPID} '{if ($2 == BGPID) print "Process with PID '"$BGPID"' exists"}')
    if [[ ${PR_EX} = "Process with PID $BGPID exists" ]]; then
       echo "Waiting for the Process with PID ${BGPID} to complete its execution"
       echo "Sleeping for 5 Seconds"
       sleep 5
       continue
    else
       echo "Process with PID ${BGPID} executed successfully"
       break
    fi
done
 

I appreciate your answer only thing how can i pass parameters now.

Post the real system command (with parameters) you are using and I might be able to translate it to my solution.

Wouldn't it be possible to do like this:

 
system("/path/to/your/script.sh $machine_name $path_of_file $destination  &")
 
 

script.sh will contain something like this:

#!/bin/sh
MACH_NAME=${1}
PATH_FILE=${2}
DEST_PATH=${3}

scp -r ${MACH_NAME}:${PATH_FILE} ${DEST_PATH}
mailx ...