Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Sz command hanging and outputting ZNAK’s

by M1k3 (Initiate)
on May 18, 2011 at 09:24 UTC ( [id://905442]=perlquestion: print w/replies, xml ) Need Help??

M1k3 has asked for the wisdom of the Perl Monks concerning the following question:

OS – Linux


I have a Perl script which exec’s the sz command to send files over modems (Z MODEM protocol). In some instances (for example when the power goes out on the receiving modem) the sz command hangs and ZNAK errors are written to a log file causing the process to hang and log file to rapidly grow. For example:


Retry 0: Bad CRC
Retry 0: Got ERROR
Retry 0: Bad CRC
Retry 0: Got ERROR
Retry 0: Got ZNAK
Retry 0: Got ZNAK
Retry 0: Got ZNAK
Retry 0: Got ZNAK

How can I trap for such errors/terminate the process as soon as there is a problem? Is there a parameter I can pass?


Sample code (exec $cmd is where it hangs):

my $gSendCommand = "sz -b -u" my $ret = Run("$gSendCommand $gUploadFile", 1); sub Run # ($cmd, $serial) { my ($cmd, $serial) = @_; my $ret = fork; ptUtil::fatal("Could not fork to run $cmd") if $ret < 0; if ($ret == 0) { if ($serial) { my $fileno = $gSerial->{FD}; my $flags = 0; # redirect STDIN and STDOUT to modem open(STDIN, "<&=" . $fileno) or ptUtil::fatal("Could not +dup2 STDIN!"); open(STDOUT, ">&=" . $fileno) or ptUtil::fatal("Could not +dup2 STDOUT!"); # enable STDIN blocking if (fcntl(STDIN, F_GETFL, $flags)) { $flags &= ~O_NONBLOCK; fcntl(STDIN, F_SETFL, $flags); } # enable STDOUT blocking if (fcntl(STDOUT, F_GETFL, $flags)) { $flags &= ~O_NONBLOCK; fcntl(STDOUT, F_SETFL, $flags); } } # exec command ptUtil::log("Executing $cmd ($?)"); exec($cmd) or ptUtil::fatal("Could not exec $cmd"); ptUtil::log("Executed $cmd ($?)"); } wait; return $?;

Replies are listed 'Best First'.
Re: Sz command hanging and outputting ZNAK’s
by anonymized user 468275 (Curate) on May 18, 2011 at 13:30 UTC
    You could use IPC::Open3 (saving the child pid it returns) to run the command and read its output from filehandles. Then if you don't like what it is doing on that basis, closing the filehandles will terminate the child and the parent can waitpid the pid of the child before iterating the next remote device cleanly.

    One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://905442]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found