1832979 Members
3675 Online
110048 Solutions
New Discussion

Shell script

 
SOLVED
Go to solution
Sharvil Desai
Frequent Advisor

Shell script

Hi,
What I have to do is,
write a shell-script that keeps on checking whether the file test.txt exists, and once it does exist then load that in the database.
I have accomplished that by doing,

while ! test -s data.txt
do
sleep 120
done
if test -s data.txt
then
#load the data in database
fi

However, I also have to put in a condition in the while loop saying keep on checking for test.txt for three hours, but if it's not there in 3 hours then stop checking for that file and get out of the loop.

Can someone please provide some feedback on how to accomplish this? Thank you.
"help!"
10 REPLIES 10
Sharvil Desai
Frequent Advisor

Re: Shell script

Sorry for the confusion,
Please read data.txt wherever I have said test.txt. The file I have to wait for is data.txt
Thank you.
"help!"
Rodney Hills
Honored Contributor
Solution

Re: Shell script

What if you created another script that launched your script that does a sleep for 3 hours, and when it wakes up it can kill the process if it hasn't processed the load.

script1:

while ! test -s data.txt
do
sleep 120
done
if test -s data.txt
then
#load the data in database
mv data.txt loaddone.txt
fi

script2:

sh script1 &
myjob=$!
sleep 10800
if test -e loaddone.txt
then
kill -15 $myjob
else
rm loaddone.txt
fi

HTH

-- Rod Hills

There be dragons...
Jordan Bean
Honored Contributor

Re: Shell script

# i = loop counter
# l = max number of loops
# s = sleep seconds
typeset -i i=0 l=0 s=120
let l=3*3600/$s
while [ $i -lt $l -a ! -s data.txt ]
do
sleep $s
done
if [ -s data.txt ]
then
# load
fi
John Palmer
Honored Contributor

Re: Shell script

You could simply limit the number of times that you do the loop...

let HOURS=3 #hours to run
let SLEEP=120 # seconds to check
let LOOPS="(HOURS*3600)/SLEEP"

while (( LOOPS ));
do
let LOOPS=LOOPS-1
if [[ -s data.txt ]];
then
fi
sleep ${SLEEP}
done

Regards,
John
Anil C. Sedha
Trusted Contributor

Re: Shell script

Do this

for TEST in /test.data
do
your arguments come here
if [ $TEST = 1 ]
then
exit
fi
done

Regards,
Anil
If you need to learn, now is the best opportunity
Anil C. Sedha
Trusted Contributor

Re: Shell script

Forgot to add the sleep parameter there.

put sleep 10800 after the step

your arguments come here

Regards,
Anil
If you need to learn, now is the best opportunity
Jordan Bean
Honored Contributor

Re: Shell script


another way:

sleep 10800 & # start sleeper
while kill -0 %1 >/dev/null 2>&1
do
[ -s data.txt ] && break
sleep 120
done
kill -HUP %1 # stop sleeper
if [ -s data.txt ]
then
# load
fi
Darrell Allen
Honored Contributor

Re: Shell script

Hi,

It probably won't happen often but depending on the size of data.txt, your script could try to load the database while the file is still being written.

You may want to add a check using fuser or lsof to verify the file isn't being accessed before you do your load.

An fuser example:
inuse=`fuser $file 2>&1 | awk '{print $2}'`
if [ -z "${inuse}" ]
then
echo File ${file} not in use
# load the database
fi


Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Sharvil Desai
Frequent Advisor

Re: Shell script

:-) Thank you all for promptly responding. Some solutions were very creative! I will try your approach & let you know what happened. Everyone gets a 10 for being very helpful!
"help!"
Sharvil Desai
Frequent Advisor

Re: Shell script

Hi guys,
Again thank you for your feedback. To me Jordan's first answer was the most straightforward. The logic was easy to follow, so I have decided to use that...but everyone's got their points :-) !
"help!"