Version 7 of filewait

Updated 2016-10-07 00:46:34 by RKzn

Richard Suchenwirth 2008-01-23 - Here is a little "application server" that waits for the existence of a specified file, reads it, deletes it, and executes its contents, line by line.

It can be useful to trigger some action on a remote computer to whose file system you have write access, so you can send "orders" by writing to the magic file...

No warranties, currently Windows only (use sh -c for Linux). Silly little example:

Start:

filewait hello > hello.log

Send an order:

$ echo > hello df -k .

After some seconds, the last lines of hello.log are (in my case)

 2008-01-23T14:58:18 hello: 1 > df -k .
 2008-01-23T14:58:23< 0 Filesystem           1k-blocks      Used Available Use% Mounted on
 d:                   156288320 154001928   2286392  99% /

 #!/usr/bin/env tclsh
 set usage {
    filewait.tcl -- wait for a specified file, execute its contents
    Example: filewait.tcl mycmd.txt > mycmd.log
    The program deletes the waited-for file and keeps waiting.
 }
 proc main argv {
    if {[llength $argv] != 1} {puts stderr $::usage; exit 1}
    set ::waitfile [lindex $argv 0]
    puts stderr "waiting for $::waitfile, Ctrl-C to terminate..."
    every 1000 {
        if {[file exists $waitfile]} {
            set f [open $waitfile]
            set content [read $f]
            close $f
            file delete $waitfile
            set lineno 0
            foreach line [split $content \n] {
                if {[string trim $line] eq ""} continue
                puts "[timestamp] $waitfile: [incr lineno] > $line"
                set rc [catch {exec cmd /c $line} res]
                puts "[timestamp]< $rc $res"
                flush stdout
            }
        }
    }
    vwait forever
 }
 proc every {ms body} {uplevel \#0 $body; after $ms [info level 0]}
 proc timestamp {} {clock format [clock seconds] -format %Y-%m-%dT%H:%M:%S}

 main $argv