Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I would assume that "system is out to get you", so you should just assume that trying to read files will fail, but what exactly will happen doesn't matter.

For example, when you open file or next FILE, I would instead rerun glob and try again (to protect from files removed in /usr/bin - you wouldn't want this to reboot system, right?). As for basic idea, I've something similar to your script except more... paranoic (when something will go wrong, it will reboot). You don't need to know what will happen when EIO will appear - some condition in the code (or autodie pragma) will catch something is wrong (what exactly is wrong doesn't matter).

#!/usr/bin/perl # License: Do what you want with this code snippet. You could even # claim it as yours and remove this license. I don't care. # Automatically die when something goes wrong use autodie; use constant DIRECTORY => '/usr/bin'; use strictures 1; use Try::Tiny; my @files; sub glob_files { my ($directory) = @_; opendir( my $dh, $directory ); # On my system, /usr/bin has directories such as /usr/bin/X11 return grep { -f "$directory/$_" } readdir $dh or die "Empty glob" +; } sub try_random_file { my ($file) = @_; open my $fh, '<', $file; # Four bytes should be enough to determine if stuff works read $fh, my $buffer, 4; # Shouldn't happen if ( length $buffer != 4 ) { die 'File is too small.'; } } sub reboot { my ($error) = @_; exec '/var/tmpfs/reboot', '-nf'; } @files = glob_files DIRECTORY; while (1) { try { my $file = $files[ int rand @files ]; try { try_random_file DIRECTORY . "/$file"; } catch { # If file doesn't exist then it was possibly deleted. # Reglob everything and try again with other file. If # it won't work then something is simply broken. die $_ if -f $file; @files = glob_files DIRECTORY; try_random_file DIRECTORY . "/$files[ int rand @files ]"; } } catch { reboot $_; }; # Usually, you should sleep just so script won't take entire CPU sleep 15; }

In reply to Re: I/O Watchdog Daemon by GlitchMr
in thread I/O Watchdog Daemon by IdleResonance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2014-08-29 03:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (275 votes), past polls