#!/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; }