Version 2 of A little demon

Updated 2002-08-19 12:02:07

Richard Suchenwirth 2002-08-16 - This little script checks periodically for the existance of a file at well-specified location ("todo"), and if it exists,

  • executes it in a sh (hence Unix only, for now)
  • deletes it
  • writes the result to another file ("done")

Very simple, but seems to work also with non-trivial tasks...

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

 set filename /home/suchenwi/todo
 every 5000 {
        if [file exists $::filename] {
                catch {exec sh $::filename} res
                set fp [open /home/suchenwi/done w]
                puts $fp $res
                close $fp
                file delete $::filename
        }
 }

To feed this demon, I only have to type at the shell prompt

  echo "grep fff t" > ~/todo   

and 5 sec or so later, the file ~/done contains the result of this demonery... What that is good for? Well, a colleague wanted to be able to run a program under a different account, without uid/su/rsh. This is the solution I came up with. A few days later, I needed it myself:

Printer demon: Assume a Tk app runs on Windows - its canvas can produce Postscript files, but how to print it (without starting up Ghostview or so, and manual intervention)? This solution requires that you have a directory on a Unix box (where of course just lpr brings Postscript well to paper) which is remotely mounted to your Windows box too. In my case, /home/suchenwi is mounted on Windows as x: Then I just need the following little demon:

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

 cd /home/suchenwi/etc/toprint

 every 30000 {
     foreach filename [glob -nocomplain *.ps *.PS] {
                exec lpr $filename
                file delete -force $filename
        }
 }
 vwait forever

Started with $tclsh ~/tcl/printerdemon.tcl &, it hovers in the background, and every half minute checks whether there are files in the well-known directory. On the Windows side, I just need

 $canvas postscript file x:/etc/toprint/[pid].ps

and soon enough, the pages comes out of the printer...


Arts and crafts of Tcl-Tk programming