0

I have a script that is running every 2 minutes by looking into a folder and checking if new files have been delivered the problem is that sometimes the script starts during the files are copied (the files being quite big) so an email is sent to the customer with "file x is empty" but of course the file is good. the correct message email is received afterwards.

for avoiding the overlapping between different processing I have set up a CRON_RUN file like this. If there are files that are currently processing then my script exits. when the program finishes, the CRON_RUN file is removed for allowing to the next process to be started if exists of course

Now the processing is not overlapping any more but I have observed that if I deliver a file in the input and during the copy I start the processing the program identifies an EMPTY file so it is still not as expected.

is there a command like?:

   if copy process is in run or checksum file is not completed then
   exit
   else 
   do my program
   fi

my current implementation so far is:

#!/bin/bash

   MYDIR=`dirname $0`
   ext_dir="/server/oracle/apps/delivery"

   if test -f $ext_dir/CRON_RUN
   then
      echo $0 CRON RUN >&2
      exit 1
   fi

   touch $ext_dir/CRON_RUN
   export CRON_RUN=CRON_RUN

for......
my program....

   export CRON_RUN=""
   rm -f $ext_dir/CRON_RUN

could you please tell me if I understood correctly how to use flock command?

#!/bin/bash

   MYDIR=`dirname $0`
   ext_dir="/server/oracle/apps/delivery"

   if test -f $ext_dir/CRON_RUN
   then
      echo $0 CRON RUN >&2
      exit 1
   fi

   touch $ext_dir/CRON_RUN
   export CRON_RUN=CRON_RUN

#running the program for each input folder
#inside the input some files might be in copying
#if a process copying is in progress program should exit
# if not then the load-input script should start

for input_folder in electro food comp rof 
do

(
# Wait for lock on /server/oracle/apps/delivery/$input_folder/.load-input.exclusivelock(fd 200) for 10 seconds
flock -x -w 10 200 || exit 1

# Do stuff

$MYDIR/load-input $input_folder  $mylog > $mylog 2>&1

) 200>/server/oracle/apps/delivery/$input_folder/.load-input.exclusivelock
  done
export CRON_RUN=""
rm -f $ext_dir/CRON_RUN
radu
  • 69
  • 4
  • 1
    Use a lock file and check for that in your script. If it exists, just exit, otherwise run. – arco444 Sep 26 '14 at 12:23
  • I don't know if you have skills on C/C++, but if yes, you can use this library : http://stefan.buettcher.org/cs/fschange/ – Quentin THEURET Sep 26 '14 at 12:51
  • I don't have skills even for bash scripting but I have to do it. – radu Sep 26 '14 at 13:00
  • 2
    Look at this [flock](http://stackoverflow.com/a/169969/1563512) answer. This is the general way to do it. – zerodiff Sep 26 '14 at 13:43
  • @zerodiff: is this OK? I just built the solution – radu Sep 29 '14 at 09:30
  • That looks correct to me, if you want to lock on the `load-input` part. You can also use `flock` once for the whole script, if the intent is to prevent multiple scripts from running at the the same time, although your `$ext_dir/CRON_RUN` seems to do this. So, you are using `flock` correctly, but it depends on what part of the script you want to lock. If you want to lock the whole script, you could use flock instead of the `CRON_RUN` file and take the `flock` out of the loop, if that makes sense. – zerodiff Sep 29 '14 at 16:10

0 Answers0