home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / perl / 7077 < prev    next >
Encoding:
Internet Message Format  |  1992-11-20  |  3.7 KB

  1. Xref: sparky comp.lang.perl:7077 comp.unix.admin:6299
  2. Path: sparky!uunet!stanford.edu!unix!unix.sri.com!dowding
  3. From: dowding@ai.sri.com (John Dowding)
  4. Newsgroups: comp.lang.perl,comp.unix.admin
  5. Subject: Difficulties running a Perl/tip job from cron
  6. Message-ID: <DOWDING.92Nov20113240@palm.ai.sri.com>
  7. Date: 20 Nov 92 19:32:40 GMT
  8. Sender: news@unix.SRI.COM
  9. Followup-To: comp.lang.perl
  10. Distribution: comp
  11. Organization: SRI International, Menlo Park, CA
  12. Lines: 157
  13.  
  14.  
  15. Quick summary:  I have Perl program that spawns a modified version of
  16. tip that works fine when called from the csh command line, but fails
  17. when called from cron.
  18.  
  19. The details: I want to run a daily program that logs into a remote
  20. (non-unix site), runs a variety of commands to download some
  21. information (OK, basketball box scores), and exits.  I hacked up the
  22. tip source to rip out the raw-mode stuff, so that tip could be run on
  23. the ends of a pipe.  I then wrote a Perl script to spawn the tip
  24. process, then issue a series of wait-for-prompt/send-string commands,
  25. and send me email with the downloaded information when it finishes.
  26.  
  27. This script works fine when called from the csh command line, but
  28. when called from cron fails with an infinite loop of 
  29. "can't synchronize with hayes" messages.
  30.  
  31. The only obvious cron side-effect I know of is that the PATH needs to
  32. be set in the script.  I believe that I have done this correctly, but
  33. please let me know if there is anything I am leaving out.
  34.  
  35. I have included the relevant portions of the script I am using below,
  36. any help is appreciated.
  37.  
  38. John Dowding
  39. dowding@ai.sri.com
  40. ---------------------------------------------
  41. #! /usr/local/bin/perl
  42.  
  43. $ENV{'PATH'} = "/usr/ucb:/usr/bin:/usr/local/bin";
  44.  
  45. $TIPSTRING = "/usr/local/bin/batch_tip tymnet96";
  46.  
  47. # open a pair of pipes
  48. pipe(FROMPARENT,TOCHILD);
  49. pipe(FROMCHILD,TOPARENT);
  50.  
  51. # make all pipe handles unbuffered
  52. foreach $f ("FROMPARENT","TOCHILD","FROMCHILD","TOPARENT") {
  53.   select($f); $|++;
  54. }
  55. select(STDOUT);
  56.  
  57. # Fork or die
  58. $child = fork;
  59. die "Cannot fork: $!" unless defined $child;
  60.  
  61. # Child code:
  62. unless ($child) { 
  63.  
  64. # close the other ends
  65.   close(FROMCHILD); 
  66.   close(TOCHILD); 
  67.  
  68. # Reset STDIN
  69.   close(STDIN);
  70.   open(STDIN,"<&FROMPARENT");
  71.   close(FROMPARENT);
  72.  
  73. # Reset STDOUT
  74.   close(STDOUT);
  75.   open(STDOUT,">&TOPARENT");
  76.   close(TOPARENT);
  77.  
  78.   exec $TIPSTRING;
  79.  
  80.   exit 0;
  81. }
  82.  
  83. # Now in Parent
  84.  
  85. # close the other ends
  86. close(FROMPARENT); 
  87. close(TOPARENT); 
  88.  
  89. #### parent code goes here, reading from FROMCHILD, writing to TOCHILD
  90.  
  91. # Unset Input record separator
  92. undef $/;
  93.  
  94. # Allow multi-line patterns
  95. #$* = 1;
  96.  
  97. open(MAIL, "| mail -s 'Daily Boxes' dowding");
  98.  
  99. $mail = 1;
  100. # Set a timeout alarm and alarm-handler
  101. alarm(300);
  102.  
  103. $SIG{'ALRM'} = 'alarm_handler';
  104. $history_line = 0;
  105.  
  106. &waitfor("log in: ");
  107. &send("linc");
  108.  
  109. # Long series of &waitfor and &send commands deleted.
  110.  
  111. &send_chars("~.");
  112.  
  113. close(TIP);
  114.  
  115. close(MAIL);
  116.  
  117. exit;
  118.   
  119.  
  120. sub alarm_handler {
  121.     print MAIL "Unable to download boxes today\n";
  122.     close(MAIL);
  123.     exit;
  124. }
  125.  
  126. # waitfor ($string) -- reads output from tip until $string has occurred
  127. sub waitfor {
  128.   local ($string) = @_;
  129.   $byte_counter = 0;
  130.   while (1) {
  131.     $bytes_read = read(FROMCHILD, $_, 1,$byte_counter);
  132.     $byte_counter += $bytes_read;
  133.     if (/\n/) {
  134.       $history[$history_line++] = $_;
  135.       print $_;
  136.       if ($mail) {
  137.       print MAIL $_;
  138.       }
  139.       $byte_counter = 0;
  140.       $_ = "";
  141.     }
  142.     if (/$string/) {
  143.       print $_;
  144.       if ($mail) {
  145.       print MAIL $_;
  146.       }
  147.       last;
  148.     }
  149.   }
  150. }
  151.  
  152.  
  153. # send string to child, followed by a ^M (octal 15)
  154. sub send {
  155.   local ($string) = @_;
  156.  
  157.   print TOCHILD $string,"\015";
  158. }
  159.  
  160.  
  161. # send chars, but do not send the ^M
  162. sub send_chars {
  163.   local ($string) = @_;
  164.  
  165.   print TOCHILD $string;
  166. }
  167.  
  168. --
  169. John Dowding
  170. dowding@ai.sri.com
  171.