Snippets

Brian Medley Check for yum updates and email if any found

Created by Brian Medley last modified
#!/opt/perl

use Email::Sender::Simple qw(sendmail);
use Email::Simple;

use Sys::Syslog;
use Sys::Hostname;

use IPC::System::Simple qw(run);

use Fcntl qw(LOCK_EX LOCK_NB);

open(my $fh, '<', $0) or die("Unable to open $0");
if (! flock($fh, LOCK_EX | LOCK_NB)) {
    die("another instance is running");
}

eval {
    my $cmd = "/usr/bin/yum --quiet check-update 1>/dev/null";

    if (run([0,100], $cmd) == 100) { 
        mail_me();
    } else {
        info("no updates");
    }
};
if ($@) {
    info("issue: $@");
}

sub mail_me {
    my $mail = Email::Simple->create(
        header => [
            To => readlink("$ENV{HOME}/email"),
            From => sprintf('yum_updates@%s', hostname),
        ],
        body => "There are yum updates available",
    );

    sendmail($mail);
}

sub info {
    openlog("yum_chk", 'cons,pid', 'user');
    syslog('info', @_);
    closelog();
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.