Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wget concurrent execution!!!

Status
Not open for further replies.

unix81

Technical User
Jun 27, 2003
4
0
0
MX
Hi,
Im using wget to retrieve information via ftp every 2 hours from a server ( using UNIX crontab command). I´d like to know what will happen if the next wget executes while the other is still mirroring data. How can I avoid any possible error?

Thats all, hope somebody can help me
 
Look for a wget pid and hold off till the original finishes
with a script internal max timeout value for error is my best guess.

Not too hard.
If you need an example write back.
 
thanks marsk , thats what i was thinking but i´d really appreciate if you could post an example. Im new to this stuff.
 
I'm not going to write the entire script for you but I'll
post some ideas.

Code:
#function to lookup and return pid's based on program name
#usage: lookupId wget ; var=`lookupId wget`
function lookupId() { 
prog=$1       
   all=`ps -aux | grep $prog | awk ' { if ($0 !~ /grep/) {print $2}}'`     
   if [ ! -z "$all" ] ; then         
      echo $all     
   fi 
return
 }

#function to monitor and kill a running process.
#usage: sleepCheck 3600 60 wget
function sleepCheck() {
tbin=$PWD/tt
max=$1
interval=$2
prog=$3
start=`exec $tbin`
now=`exec $tbin` 
pidlist=`lookupId $prog`
     
         while :
         do
               etime=`expr $now - $start`
               if [ $etime -gt $max ] ; then
                  for all in $(echo $pidlist)
                  do
                       kill -kill $all || echo "Unable to kill $all"
                  done
                  exit 0 > /dev/null
               fi
               sleep $interval
               pidlist=`lookupId $prog`
               if [ -z "$pidlist" ] ; then 
                   echo  "No processes for $prog found"
                   exit 0 > /dev/null
               fi
               now=`exec $tbin`
         done
} 

#snippet to generate a program for epoch time
#usage: genStamp
function genStamp() {
binp=$PWD/tt
rfile=$PWD/raw.c

   if [ -e $binp ] ; then
      return 
   fi
   
     cat << EOF > $rfile
     #include <stdio.h>
     #include <time.h>  

     int main(void) {
              printf(&quot;%d\n&quot;,time(NULL));
     return 0;
     }
EOF
gcc $rfile -o $binp || echo &quot;Could not create $binp&quot;
rm $rfile
}

Functions are only basically tested.
The sleepCheck function may require some syntax
adjustments.

Hope this helps you.
 
Thanks a lot marsd!
That was all i needed.
 
Maybe I'm missing something, but...

how about:

#!bin/sh
retreiveFtpInfo & # run FTP function in the background
retrieveFtpPid=$! # capture pid of last background process

then,

kill -9 $retrieveFtpPid # to kill the process if necessary

-or-

grep on $retrieveFtpPid and use an 'if' test to sleep until it's finished.

Duane
 
Thanks Duane, that was an interesting approach, and it works!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top