in reply to Perl script in cron

bashi,

Perhaps you are relaying on environment variables, or perhaps this is a permissions based issue, until you post your code for us to see it is hard to tell. Please read the PerlMonks FAQ and How do I post a question effectively?. Also did you Super Search this topic before posting? Cron questions seem to pop up quite often, e.g. Need Help regarding perl script in unix cronjob...

Hope this helps

Martin

Replies are listed 'Best First'.
Re^2: Perl script in cron
by bashi (Acolyte) on Dec 19, 2006 at 09:43 UTC
    Thanx for a quick answer. Im sorry Im not acustom with how this site works. Will make sure to be on my best behavior next time. You may already have answered my question though. I do rely on $ENV{'HOME'} when you menssioned it. Gonna check that out. Thanx again.

    /Roger
      bashi,

      don't worry about your behaviour, you have not upset anyone here. Welcome to the Monastery, I am sure you will get used to this place soon :).

      Cheers,

      Martin
        OK, Im back, this time with the small newbie script Ive put together. Ive removed the $ENV{} for roots homedir and added the full path instead but I still get error att line 43(marked with comment and <---). Ive search on the site but cant seem to get any clues on whats going wrong here.

        #!/usr/bin/perl # # getsysinfo.pl - via ssh, get systeminformation about servers. # Note: This script depends on DSA/RSA key authentication # as we dont want passwords in the actual script so # you need to make sure you got this set up correctly # before this script will work. use DBI; use Net::SSH::Perl; $username = 'root'; %db = ( database => 'sysinfo', hostname => 'localhost', username => 'boogo', password => 'xxxxx'); # Hostnames of servers to check @servers = ('nw04','nw05','gw01'); # Commands for the checks to be executed. %checks = ( "disk" => 'df -h', "mem" => 'free -mot', "pci" => 'lspci -v', "net" => 'ifconfig' ); %results = {}; # Set up mysql connection $dsn = "DBI:mysql:database=$db{'database'}:host=$db{'hostname'}"; $db = DBI->connect($dsn, $db{'username'}, $db{'password'}) or die ("$!\n"); # lets walk the @servers array and do the tests and save results. foreach $server (@servers) { $ssh = Net::SSH::Perl->new($server, debug=>0, identity_files=>["/root/.ssh/id_rsa.pub"], protocol=>'2,1'); $ssh->login($username); # <--- ERROR returned +from cron to root's mailbox while (($key, $value) = each(%checks)) { if (&db_host_exists($server) > 0) { # host already exists in database, lets update it. ($stdout, $stderr, $exit) = $ssh->cmd($checks{$key}); $sth = $db->prepare(qq{ UPDATE servers SET $key='<pre>$stdout</pre>', updated=NOW() }); $sth->execute(); $sth->finish; } else { # host does not exists in database, lets insert it. ($stdout, $stderr, $exit) = $ssh->cmd($checks{$key}); $sth = $db->prepare(qq{ INSERT INTO servers (id, hostname, updated, $key) VALUES ('','$server',NOW(),'<pre>$stdout</pre>') }); $sth->execute(); $sth->finish; } } } # db_host_exists($) - subroutine to check if the host allready # exists in the database. Then we use update, # otherwise insert. # Arguments: $ - hostname to look for sub db_host_exists($) { $result = 0; $sth = $db->prepare(qq{ SELECT * FROM servers WHERE hostname LIKE '%$server%' }); $sth->execute(); if ($sth->rows > 0) { $result = 1; } $sth->finish; return $result; }

        Guess you monks will see my error right away.

        My cron line looks like this:
        0,30 * * * * /root/bin/getsysinfo.pl


        /Roger