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

Have you got time? 1

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
Hello,

I'm sure this has been answered before, but I can't see anything relevant from a keyword search.

I have a script that runs some batchwork and has various phases.

The batchwork should start at 03:30 from cron, but may not always as it's dependant on files being FTP'd from another server. I'm trying to write something to tell me

1. The start & end time of the process - This works
2. The time each phase started finished - This works
3. The Total time in hh:mm:ss the process took
4. The Total time each phase took

The results I'm getting at the moment are things like

04:84:66 total time

Anyone got any useful tips on taking one time from another and getting a decent output?



--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Found this

{
h1=`echo $1 | cut -c1-2` # Get Start Hour
m1=`echo $1 | cut -c3-4` # Get Start Minute
s1=`echo $1 | cut -c5-6` # Get Start Second
h2=`echo $2 | cut -c1-2` # Get Stop Hour
m2=`echo $2 | cut -c3-4` # Get Stop Minute
s2=`echo $2 | cut -c5-6` # Get Stop Second
s3=`expr $s2 - $s1` # Calculate Second Difference
if [ $s3 -lt 0 ] # Test for Negative Seconds
then
s3=`expr $s3 + 60` # If yes - add one minute...
m1=`expr $m1 + 1` # ... and to subtractor
fi
m3=`expr $m2 - $m1` # Calculate Minute Difference
if [ $m3 -lt 0 ] # Test for Negative Minutes
then
m3=`expr $m3 + 60` # If yes - add one hour...
h1=`expr $h1 + 1` # ... and to subtractor
fi
h3=`expr $h2 - $h1` # Calculate Hour Difference
if [ $h3 -lt 0 ] # Test for Negative Hours
then
h3=`expr $h3 + 24` # If yes - add one day
fi
for number in $h3 $m3 $s3 # Loop through numbers...
do
if [ $number -lt 10 ] # If number is single digit...
then
/usr/5bin/echo "0$number\c" # ... add leading zero
else
/usr/5bin/echo "$number\c" # ... else - don't
fi
done
echo "" # Terminate the string
}

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
I think I am 3 for 3 on answering a variation of the time question. First, you will need to parse the time out, because it is based on 60 not 10. So that is good.

However if the real issue is waiting for the ftp file to show up, try this: (Korn shell)
#
#!/bin/ksh
#
TIMESTAMP="/tmp/.mytimestamp"
WAITINGON="/home/ftpfile"
# mark file with start of day time/date
touch -t $(date +%Y%m%d0000) $TIMESTAMP
#
COMPLETE="no"
until [[ $COMPLETE = "yes" ]]; do
if [[ $TIMESTAMP -nt $WAITINGON ]]
then
sleep 600 # 10 minutes
else
COMPLETE="yes"
fi
done
...remainder of process

This way you will start the process and if your file is not there yet, sleep and look again. Once it gets there, off you go. I strongly suggest you put an extra check in that if the file does not show up in a given amount of time it sends an alert/email/notice/etc. [/B]
 
write your script with functions, when you call your functions at the bottom, stick the command 'timex' in front of the function. Also, call your script with 'timex' in front. The value of "real" is what your looking for.

crowe
 
Thanks for the answers ,but you've both missed the point.

Gamerland

my script to wait for FTP works and runs when it's found the ok file

crowe

I don't want to use timex.

I want to take one time from another

eg

start = 03:00:00
P1 fin = 03:12:00
time taken start to P1 fin = 00:12:00

P2 start = 03:12:01
P2 fin = 04:58:33
time taken P2 start to P2 fin = 01:46:32

Etc

I think I've worked out how to do it, but wondered if anyone had a simple solution.







--
| Mike Nixon
| Unix Admin
|
----------------------------
 
#----define some functions
function convert_time_to_seconds {
echo $1|tr ':' ' '|read hrs min sec
echo $((($hrs*60*60)+($min*60)+$sec))
}
function convert_seconds_to_time {
hrs=$(($1/(60*60)))
min=$((($1-($hrs*60*60))/60))
sec=$(($1 % 60))
printf "%02d:%02d:%02d\n" $hrs $min $sec
}

#----example using times
start_time="03:00:00"
start_secs=$(convert_time_to_seconds $start_time)
end_time="04:12:57"
end_secs=$(convert_time_to_seconds $end_time)
elapsed_secs=$(($end_secs-$start_secs))
elapsed_time=$(convert_seconds_to_time $elapsed_secs)
echo eg1. time taken $elapsed_secs seconds
echo eg2. time taken $elapsed_time

#----examples using built-in variable $SECONDS
timer1=$SECONDS
sleep 5
timer2=$SECONDS
echo eg3. time taken $(convert_seconds_to_time $(($timer2-$timer1)))
timer3=$SECONDS
sleep 6
timer4=$SECONDS
echo eg4. time taken $(convert_seconds_to_time $(($timer4-$timer3)))
echo eg5. total time $(convert_seconds_to_time $(($timer4-$timer1)))


eg1. time taken 4377 seconds
eg2. time taken 01:12:57
eg3. time taken 00:00:05
eg4. time taken 00:00:06
eg5. total time 00:00:11
 
Sorted

Thanks Ygor



--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top