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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sleep time limit 2

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
0
0
US
ksh - newbie to UNIX, looking to modify existing script. Synopsis of the code:

if [ -s $DATA_DIR/file.dat ]
then...

The script works fine, but I'd like to add an else clause to the end that will basically say, if the file.dat does not exist, sleep for 15 minutes and check for it again. Keep doing this until 10:00AM...if it's still not there, then stop checking and send me an email.

else
sleep 900

but then what?
 
something like

...
else
sleep 900
now=`date +'%H%M'`
if test $now -gt 1000
then
echo "No file found at `date`"|mail -s "warning subject" me@mydomain.com
fi
fi

HTH,

p5wizard
 
of course it also needs a way out of the loop:

...
else
sleep 900
now=`date +'%H%M'`
if test $now -gt 1000
then
echo "No file found at `date`"|mail -s "warning subject" me@mydomain.com
exit
fi
fi

HTH,

p5wizard
 
Thanks wiz...would I need that "exit" if I've already got an exit statement at the end of the script? I mean, will the rest of the script (after this portion) be executed if I have that exit and finish there?
 
put a "break" instead, the script will continue whatever's after the loop

while true
do
if [ -s $file ]
then
...
else
sleep 900
now=`date +'%H%M'`
if [ $now -gt 1000 ]
then
... mail ...
break
fi
fi
done

<script continues here>

HTH,

p5wizard
 
I need to revisit this topic, as I never did get this working properly.

I want to first check to see if the file is not there (prior to checking if it is there)...have it perform the time test and send the email if the time has passed. If it hasn't passed, then sleep and come back to perform the test again.

Right now, all works well until it sleeps. Then when it comes back, it's not doing anything but ending the program. I don't know how to tell it to perform the test again. Here's what I've got:

Code:
if [ ! -s $DATA_DIR/file.nam ]
then

now=`date +'%H%M'`
	if test $now -gt 1000
 	 then   
   	 /usr/bin/echo $MESG_DELAY | /usr/bin/mailx -s $MESG_DELAY email@domain.com

	else  

	sleep 900
	
fi

else

if [ -s $DATA_DIR/file.nam ]
...
 
Seems you didn't use a while ... do ... done loop.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I don't understand how that works...so if I write something like this:
Code:
while true
do

if [ ! -s $DATA_DIR/test2.das ]
then

now=`date +'%H%M'`
	if test $now -gt 1032
 	 then   
   	 /usr/bin/echo $MESG_DELAY | /usr/bin/mailx -s $MESG_DELAY david.stinsman@certegy.com

	else  

	sleep 120

	fi
done

I'm getting the error: "ksh: syntax error: `done' unexpected
 
You have two if and only one fi
Furthermore how do you expect to exit the loop ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OK, how can I kill this process? It's going to take down my dev db...
 
It should be spending 99.99% of its time sleeping, so it should have no impact to your system, performance-wise.

Search your ps -ef listing for the parent shell process and kill that. If you ran it against your terminal try ps -ft pts/123 for example (use the tty command to see what terminal you are on).

What operating system exactly?

Annihilannic.
 
It's a UNIX box...well, I ended the process by creating the file that was being tested.

Which brings me to my next stupid question...how do I add an exit for the loop?
 
Try this logic...
Code:
#----wait for file to arrive, but time out at 10am
while [[ ! -s $DATA_DIR/test2.das && $(date '+%H:%M') < 10:00 ]]
do
    sleep 900
done

#----check for file
if [[ -s $DATA_DIR/test2.das ]]
then
    : file arrived, do something
else
    : must have timed-out, so send email
fi
 
Ygor, I think this is working for me...thanks!

Question, I did some reading in MAN KSH and I'm trying to figure out what the && means. So this is basically saying if the first part is true (test2.das is not there) then continue to the second part (date and time). Is that correct?
 
OK, just found it:

expression1 && expression2
TRUE, if expression1 and expression2 are both TRUE.
 
stinsman said:
Question, I did some reading in MAN KSH and I'm trying to figure out what the && means. So this is basically saying if the first part is true (test2.das is not there) then continue to the second part (date and time). Is that correct?

expression1 && expression2
TRUE, if expression1 and expression2 are both TRUE.

Both of those statements are correct stinsman; because of the evaluation order, if the first part is false logically the result of the entire expression is going to be false, so the shell doesn't even bother executing the second part.

Instead of writing "if" statements some people script like this, for example:

Code:
[[ -s $DATA_DIR/test2.das ]] && {
    : file arrived, do something
} || {
    : must have timed-out, so send email
}

Annihilannic.
 
Gotcha...and in the case of that particular piece of code, the two pipes are essentially "OR" right?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top