I am currently writing a bit of code in tcl that is suppose to sit between a scheduling system and a batch processing system to be able to get the scheduling software to fully understand what the batch processing is doing. The problem is that I needed the output from the command to be outputted to the terminal as soon as the information is written to stdout by the batch processing. Sorted this out by using open “| command to process 2>@ stdout” then using fileevent and vwait to write to screen. Now comes the problem, how to get the error code from a command that runs in an open pipe. [catch ] does not work as it seems like catch will only catch the output from open and not from the command it self. Tried to look into bgexec, but that is not available in the version of tcl we use (and I can not upgrade the tcl interpreter). Any suggestions.
Bellow is the source code, test is just a file that prints some stuff to stdout and stderr, with a five second delay, then returns a return code of four.
Test2 will try to call “test” using open | test …., then set a fileevent followed by a vwait.
Code for "test"
I guess I do not really have to point out that this is actually not output from our batch management system, even though it may make slightly more sence then the batch management system
and the tcl code that I need to change to get the return code.
Bellow is the source code, test is just a file that prints some stuff to stdout and stderr, with a five second delay, then returns a return code of four.
Test2 will try to call “test” using open | test …., then set a fileevent followed by a vwait.
Code for "test"
Code:
echo "I'm not really an abwroke you know"
sleep 5
echo "You said $1"
sleep 5
echo "Is that a bad word" >&2
sleep 5
echo "This conversation is over"
exit 4
and the tcl code that I need to change to get the return code.
Code:
#!/apps/TclPro1.4/solaris-sparc/bin/expect -f
proc Write_output { pHandler } {
global command_running
flush $pHandler
set line [ read -nonewline $pHandler]
if {[ string length $line ] >= 0} {
puts "OUTPUT :\"$line\""
}
if {[eof $pHandler]} {
close $pHandler
set command_running "1"
}
}
proc ParExec { command_line } {
global command_running
set command_running 0
puts "Im going to exec $command_line"
#set pHandler [ open "|[info nameofexecutable] $command_line" ]
set pHandler [ open "| $command_line 1>@ stdout 2>@ stdout" r+ ]
puts "$pHandler "
fconfigure $pHandler -blocking 0 -buffering line -translation crlf
fileevent $pHandler readable [ list Write_output $pHandler ]
vwait command_running
}
ParExec "./test hello"