I am new to perl (5 days as of today), and I seek the wisdom of the perl monks. I am writing a script, run from cron, that monitors whether or not a host on the network is pingable. I think I have the the core part of the script functioning well, but I am unsure of some of the other parts. As documented in the code, my first concern has to do with the creation and checking of a lock file used to detrmine if the previous execution of the script finished. My second concern is of the system calls made and general perl worthiness of the code. To sum up, I'd like some peer review of my first REAL baby. (note: this code is based off of someone else's previous work, but is entirely re-written to be more efficeint, (I hope)). Thanks for your time.
#!/usr/bin/perl -w # Run this perl code from cron every 5 minutes # 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /.ping_hosts.pl 2>&1 # # Problem: Any user on localhost can stop the repeated execution # of this script by creating the lock_file. If I create a # non-predictable name for the lock file, how does the script know # what the previous lock_file name was? $lock_file = "/tmp/ping_hosts.lock"; sub test_for_lock { # Check if last run has finished my $lock_file = shift(@_); if (-e $lock_file) { system("touch $lock_file"); die "Process lock file, ${tattle}, still exists (Old process s +till running?)\n"; } else { system("/bin/touch $lock_file") } } sub ping_hosts { my $timeout = 2; my @hosts = qw(host1 host2 host3 host4); foreach $ws (@hosts) { $result = qx(/usr/sbin/ping $ws $timeout); unless ($result =~ m/alive/) { print "$ws did not respond to ping within the $timeout sec +ond timeout period\n"; notify_admins($ws); } } } sub notify_admins { # Called from ping_hosts() # Similar problem with $nfile as with $lock_file my $nfile = "/tmp/${ws}.notify"; my $ws = shift(@_); my $admins = "admin1\@mailhost.com, " . "18005551212\@page.com, " . "admin2\@mailhost.com, " . "19005551212\@page.com"; # root, aka script owner, already getting notified by cron unless (-e $nfile) { open(MAIL, "|/bin/mailx $admins") or die "Can't fork for mailx +: $!\n"; print MAIL "$ws doesn't respond to ping\n"; close(MAIL); system("/bin/touch $nfile"); } } ### MAIN ### test_for_lock($lock_file); ping_hosts(); system("/bin/rm $lock_file");

In reply to lock files vs. non-predictable file names by RuphSkunk

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!
  • 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, details, 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, summary, 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.