Operating System - HP-UX
1752379 Members
6120 Online
108788 Solutions
New Discussion юеВ

Re: Help with writing a cron script

 
Suman_7
Frequent Advisor

Help with writing a cron script

All,

I need to write an Inifinite Loop in Korn Sheel. This script is supposed to kick off every day at around 12:00 PM.

I wrote the script like this:
###################################################################

export FILE_DIR=$TOP/bin

if [ $# = 0 ]; then
interval=86400
elif [ "${1}" = "fast" ]; then
interval=900
else echo "Incorrect usage........."
echo "Execute without parmeter or with parameter 'fast' to execute in fast mode"
exit 1
fi

t=1000

while [ ${t} -le 1001 ];
do
switch_val=`head -1 auto_on_off.txt | awk '{print $1}'`

if [ "${switch_val}" = "on" ]; then
${FILE_DIR}/run_load_A_B.ksh db1 db2 db3
${FILE_DIR}/run_load_C.ksh db1 db2 db3
#sleep 86400
sleep ${interval}
echo "condition not yet fulfilled run again after 24 hrs"
else
a=1
#exit 1
fi
done



My Problem is that the scripts:

run_load_A_B.ksh
run_load_C.ksh
take 4-5 hours to run .

once they complete the script sleeps for 24 hours. and next dat it executes at the finish time of previois day.
How do I make sure it runs at a specific time each day provided previous day's run was completed.


The script runs in 2 modes normal and fast mode. I am concerned abt the normal mode ( every 24 hours )


Please suggest any good logic for this.

Thank You,




7 REPLIES 7
curt larson_1
Honored Contributor

Re: Help with writing a cron script

the easiest way is just to let cron run the job everyday at 12.

0 12 * * * command
curt larson_1
Honored Contributor

Re: Help with writing a cron script

and if you like computing the wait time yourself

You can download caljd.sh or caljd.pl from here:

http://www.cmve.net/~merijn/#Contrib

It is at the bottom of the page.


YESTERDAY_DATE=$(caljd.sh -s -y $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY_DATE}"
TOMORROW_DATE=$(caljd.sh -s -y $(caljd.sh -n 1))
echo "Tomorrow = ${TOMORROW_DATE}"

Invoke as caljd.sh -u for full usage and many examples (like previous date plus skip over weekends).
KapilRaj
Honored Contributor

Re: Help with writing a cron script

y don't you let it run in the back ground ?.

command1 &
command2 &
sleep
Nothing is impossible
Suman_7
Frequent Advisor

Re: Help with writing a cron script

Thanks for the reply , but I can't use cron job as this is the requirement.

I can't let it run in the background because the users want the ability to turn it on and off as desitred by updating the auto_on_off.txt file to "on" or "off"


Thanks
Stuart Browne
Honored Contributor

Re: Help with writing a cron script

You've already got the logic in there for the auto_on_off, so if they say 'off', it won't run.

Now, the executing of the script 1000 times can also be gotten rid of.

Simply, you could shorten the entire routine to something like this:

###########
# Start here

export FILE_DIR=$TOP/bin
LOCK_FILE=$0.lock

if [ ! -f "$LOCK_FILE" ]
then

touch "$LOCK_FILE"
switch_val=`head -1 auto_on_off.txt | awk '{print $1}'`

if [ "${switch_val}" = "on" ]; then
${FILE_DIR}/run_load_A_B.ksh db1 db2 db3
${FILE_DIR}/run_load_C.ksh db1 db2 db3

##
# Insert logic here checking for failures. If failed, don't do the next line:

if [ .... ]
then
rm -f "$LOCK_FILE"
fi
else
echo "Lock file still in place. Either a previous execution failed, or I was run multiple times."
fi

######################

And cron it!
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: Help with writing a cron script

Your requirement is having some contraversies as,

You are in need to start the script execution at 12.00 PM Everyday.But the application execution time (3-4hours) + the sleep time 24 hours (86400 seconds or 900 seconds) may leed to execute the execution on next day after 12.00 PM limit.

You have to use different logic for fast mode. It is good to use the log files to know the execution time and other informations.

Attached script will be helpful for this.

Regards,
Muthukumar
Easy to suggest when don't know about the problem!
Dan Martin_1
Frequent Advisor

Re: Help with writing a cron script

Try this:

elapsed=0
while :
do
head -1 auto_on_off.txt | read switch_val therest


if [[ "${switch_val}" = "on" ]]
then
${FILE_DIR}/run_load_A_B.ksh db1 db2 db3
${FILE_DIR}/run_load_C.ksh db1 db2 db3

sleep $((interval - elapsed))
elapsed=0
echo "condition not yet fulfilled run again after 24 hrs"
else
a=1
sleep 60
((elapsed += 60))
fi
done