How to run Background process one after another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run Background process one after another
# 1  
Old 02-01-2012
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

Code:
    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?
# 2  
Old 02-01-2012
Create a script with:
Code:
#!/bin/sh
scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/
mailx ...

Then execute that script from Perl in background:
Code:
system("/path/to/your/script.sh &")

# 3  
Old 02-01-2012
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
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

# 4  
Old 02-01-2012
Quote:
Originally Posted by bartus11
Create a script with:
Code:
#!/bin/sh
scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/
mailx ...

Then execute that script from Perl in background:
Code:
system("/path/to/your/script.sh &")


I appreciate your answer only thing how can i pass parameters now.
# 5  
Old 02-01-2012
Post the real system command (with parameters) you are using and I might be able to translate it to my solution.
# 6  
Old 02-02-2012
Quote:
Originally Posted by Navrattan Bansa
I appreciate your answer only thing how can i pass parameters now.
Wouldn't it be possible to do like this:

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

script.sh will contain something like this:
Code:
#!/bin/sh
MACH_NAME=${1}
PATH_FILE=${2}
DEST_PATH=${3}

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run scripts in background one by one

Hello all, Can someone help with one script? I have as example 3 scripts 1.sh sleep 60 & sleep 60 & sleep 10 & sleep 80 & sleep 60 & 2.sh sleep 40 & sleep 5 & sleep 10 & sleep 70 & sleep 60 & 3.sh (2 Replies)
Discussion started by: vikus
2 Replies

2. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

3. Shell Programming and Scripting

how to run in background mode.

Hi All, i'm a newbie here, i'm just wondering in how do i run my script in background then echo it if it's done. Please advise, Thanks, -nik (1 Reply)
Discussion started by: nikki1200
1 Replies

4. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

5. Shell Programming and Scripting

background scripts run-help

Looking for a logic where say i have a script called parent_script which is used to call other 4 to 5 child scripts in background as.. cat parent_script # containing 65 lines 1 2 .. 35 while read child_script 36 do 37 ./child_script_name& 38 done< ${SCRIPT_LISTS} 39 40 # Need to have... (2 Replies)
Discussion started by: michaelrozar17
2 Replies

6. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

7. Shell Programming and Scripting

How to process and run a program in the background in perl?

Hi, I have a query about processing and running Perl program at the background. I have HTML file called Userform.html which accepts input from the user. As soon as input is given the contol goes to get.cgi (get.cgi does some processing and computing tasks). Actually get .cgi takes more... (0 Replies)
Discussion started by: vanitham
0 Replies

8. UNIX for Dummies Questions & Answers

can we run ssh2 in background

Hi, I was trying to run ssh2 command in background... but i got follwoing error message saying that process has been stopped.. + Stopped(SIGTTOU) Anyone have any idea about this??? Appreciated your help.. (3 Replies)
Discussion started by: pvamsikr
3 Replies

9. UNIX for Advanced & Expert Users

make a foreground running process to run background without hang up

I've tried this a long time ago and was successful but could not remember how i did it. Tried ctrl+Z and then used bg % could not figure what i did after to keep it no hangup - not sure if used nohup -p pid, can u plz help me out if this can be done. Any help will be appreciated. (12 Replies)
Discussion started by: pharos467
12 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question