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

trying to print report at the end of the day

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
I am trying to print some data at the end of the day using the "after" command but it doesn't seem to be working. Instead of just printing one line at end of day it prints several lines. There is another procedure which i haven't included where the taccepts, trequests and terrors get incremented. Can anyone point out where i am going wrong. Thanks in advance.

proc printReport {} {

global taccepts
global trequests
global terrors

puts "Accepts: $taccepts, Requests: $trequests, Errors: $terrors"
set taccepts 0
set trequests 0
set terrors 0

after [expr [expr [clock scan "today 23:59:59"] - [clock seconds]] * 1000] printReport
}

set taccepts 0
set trequests 0
set terrors 0
printReport
 
First comment: you don't need the nested exprs. expr supports arbitrarily complex arithmetic expressions, and allows parentheses to group subexpressions, just as you would in a language like C. Additionally, for subtle and complex reasons we won't get into here, expr does its own round of substitutions on its arguments. And it turns out that because of this feature and the way expr's expression parser is written, it's more efficient to quote your entire arithmetic expression in curly braces. So, you could rewrite your expression above to:

Code:
expr {([clock scan "today 23:59:59"] - [clock seconds]) * 1000}

Having said that, I wouldn't. The problem is that printReport schedules its next invocation to run at 23:59:59 "today". But you've got 1 second granularity for the clock command, millisecond granularity for the after command, and code that probably executes in just a few microseconds. I think that you're getting "caught between clock ticks," where by the time your code is ready to run the after command, your time arithmetic ends up with "0" -- and so the scheduled event runs as soon as you get back into the event loop.

So what would I do instead? Something like this (preferably allowing a bit extra time to prevent any system hiccup from delaying execution of my code until something like 00:00:03, and thus accidentally skipping a day of reporting):

Code:
after [expr {([clock scan "tomorrow 23:59:00"]
       - [clock seconds]) * 1000}] printReport

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
you're calling printReport from within the proc, printReport. What I think you want is to have the script sit there and every increment of time, or at a set time, to print 1 line. Is that right?

First off, why not use "cron" to spawn your script? That would be the simplest as it wouldn't tie up a shell. OK, let's say that's not acceptable for some reason. Have a proc that prints the data you want.

proc printReport {} {

global taccepts
global trequests
global terrors

puts "Accepts: $taccepts, Requests: $trequests, Errors: $terrors"
set taccepts 0
set trequests 0
set terrors 0

}

Then, in your main script, you need a loop in which you have your time check and call your proc. Something, perhaps, like:

set interrupt 1
while {$interrupt == 1} {
if {[expr [expr [clock scan &quot;today 23:59:59&quot;] - [clock seconds]]}<= <some number> {printReport}
after 25000;# or whatever increment you want to check
}

or something like that.

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top