Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

How to trace a dying process

by Notromda (Pilgrim)
on Sep 05, 2003 at 21:10 UTC ( #289352=perlquestion: print w/replies, xml ) Need Help??

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

I have a program that is monitoring logfiles that keeps dying mysteriously. I'm using syslog-ng which pipes information to my perl script. Is there a way to have my script do something to notify my as it gasps its last breath? I need to find some way to catch some debug information as it dies. :/

Replies are listed 'Best First'.
Re: How to trace a dying process
by pzbagel (Chaplain) on Sep 05, 2003 at 22:18 UTC

    There is a possibility that there is a lull in the stream of messages and your script reaches the end of the file per se. I know there is a module for this, but I just needed a quick script which would tail log files and then switch over to the new file when they rolled over(these logs were rolling over every 10-15 minutes), I removed the superfluous code to just demonstrate how to do the tail portion effectively:

    #Poor man's tail -f in perl open (FILE, shift) or die " Couldn't open your file."; while(1) { my $line=<FILE>; unless (defined($line)) { sleep 1; next; } # Do stuff when you actually get data }
Re: How to trace a dying process
by asarih (Hermit) on Sep 05, 2003 at 21:53 UTC
    It depends on how the script dies. If it's getting some sort of signal, then it'd be as easy as setting up %SIG. If it's dying "mysteriously," I'd pepper some print statements here and there and go from there.
Re: How to trace a dying process
by Notromda (Pilgrim) on Sep 05, 2003 at 23:46 UTC
    The script receives input on STDIN, and gets restarted when syslog restarts. Thus I can't just print anything out... though I'm attempting right now to have it write out to its own logfile in a END block.
Re: How to trace a dying process
by Notromda (Pilgrim) on Sep 05, 2003 at 21:30 UTC
    As usual, questions lead to more questions. I have code like this:
    while (<>) { ... }
    Is it possible that a EOF character is getting printed by syslog-ng and closing down the while loop?
      Not under UNIX. EOF/EOT is CTRL-D, which is ASCII 4:
      $ printf "hi\n\004\nthere\n" |perl -e'while(<>){print}' hi there
      I have no idea about other OS's, though.
Re: How to trace a dying process
by Roger (Parson) on Sep 06, 2003 at 13:59 UTC
    The while loop is a gotcha in Perl sometimes. Consider the loop
    while (<>) { ... }
    It will work most of the time, terminating when '<>' fails with undef (reaches the end of file). However when the while loop encounters a '0' by itself, it would fail as well (while loop fails on undef or '0').

    The while loop can be fix with the following:
    while (defined <>) { ... }
    This while loop will only quit when the input stream really ends.

    Now that comes to the second part of the problem, EOF's (^Z or ^D) encountered in the input stream unexpectedly. Perhaps you could switch from the while(){} loop to a do {} while () loop, and print out the input to a log file. Then you have a better chance of catching the error.
      Works fine for me:
      while (<>) { print "got $_"; }
      1 got 1 2 got 2 0 got 0 5 got 5
      Update: see perlop2:

      In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see if it is defined. The defined test avoids problems where line has a string value that would be treated as false by perl e.g. ``'' or ``0'' with no trailing newline.

      This begs an interesting question: how to detect a ^Z or ^D in a line...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-01-25 01:49 GMT
Find Nodes?
    Voting Booth?