#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |