Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2018-02-21 04:23 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (274 votes). Check out past polls.