0
votes

Need to run a piece of code for certain duration when a master job is running. Basically some tool is writing a logFile, during logfile is written i used grep for Errors/Warnings from that logFile.

I use every loop which checks for the condition of job being running and then write a Error/Warning database, if job is not running it will print message. Problem: once it founds job is running, every loop is kicked on it never stops even the else condition is false.

I used break in if loop, but didn't work. How to stop every loop, please suggest.

proc every {ms body} {
     eval $body
     after $ms [info level 0]
}

proc run_this_loop {} {
    catch {set job_name [exec qstat | awk {{print $3}} | grep liberate]} error
    if {[string match *abnormally* $error]} {
        puts "No background liberate job is found\n"
    } else {
        every 10000 {grepLoop}
    }
}

every 1000 {run_this_loop}
2

2 Answers

2
votes

One simple way is to save the id of the after invocation and use it to cancel:

proc every {ms body} {
    global id
    eval $body
    set id [after $ms [info level 0]]
}

every 2000 {puts foo}

after cancel $id

Documentation: after, eval, global, info, proc, puts, set

0
votes

I realized my bullish code.

 proc run_this_loop {} {
   catch {set job_name [exec qstat | awk {{print $3}} | grep liberate]} error
      if {[string match *abnormally* $error]} {
      puts "No background liberate job is found\n"
      } else {
     grepLoop
      }
    }

I don't need every inside else loop, removing every solves my problem. Please suggest in case any optimized way of doing it... I still needs to know how to break a every loop.