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