Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl and Cron job problem

Status
Not open for further replies.

erikbjork

Programmer
Jan 16, 2005
4
SE
Hi!

I have a code that works perfectly well when I'm running it from the command line in Linux. But when I'm running it as a cron job it doesn't seem to work. (I'm running it from /etc/crontab) I think I've figured out that it is the split function that stops working. It looks like this:

@data = split(/(.*):[ ]*(.*)°(.*)=[ ]*(.*)°(.*)=[ ]*(.*)°(.*)/);

When I'm trying to print for example $data[1] perl tells me:

Use of uninitialized value in print at /searchpath/script.pl line 80, <INPUT> line 1.

The input comes from the following line:

open( INPUT, "/usr/bin/sensors | /bin/grep \"sensor = thermistor\" |");

I can print the input lines one by one, but not split them. I print them by putting the following line just before the split command:

print($_);

Anyone who has any ideas? I have been googling around for this for a few hours.

Thank you!
 
I think it's line which fills data.

When a script is running from crontab the server is running the script without having specific environment settings.

So, can you indicate how @data is being filled?
 
I'm not realy sure what you mean, but I hope it will be easier when I post the whole script:
----------------------------------------------------------
#!/usr/bin/perl -W

use DBI;

# Database info
my $db_name = "database";
my $db_user = "user";
my $db_pass = "password";

my $string_to_fetch = "sensor = thermistor";
my $sysinfo_binary = "/usr/bin/sensors";

# Connect to the database
my $dbh = DBI->connect("dbi:mysql:$db_name","$db_user","$db_pass")
or die "I cannot connect to dbi:mysql:$db_name as $db_user - $DBI::errstr\n";

# Gather information from sensors
# Read the output from sensors
# Output the lines with temperatures

open( INPUT, "$sysinfo_binary | /bin/grep \"$string_to_fetch\" |");

while( <INPUT> )
{
chomp;

@data = split(/(.*):[ ]*(.*)°(.*)=[ ]*(.*)°(.*)=[ ]*(.*)°(.*)/);

if($data[1] eq "CPU Temp") {
my $sql="INSERT INTO mbtemp SET termID='1',Celsius='$data[2]'";
} elsif ($data[1] eq "M/B Temp") {
my $sql="INSERT INTO mbtemp SET termID='2',Celsius='$data[2]'";
}
if(defined $sql) {
$dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
}

}
close( INPUT );
$dbh->disconnect;

----------------------------------------------------------

The information I get in the INPUT is:
----------------------------------------------------------
CPU Temp: +55°C (high = +65°C, hyst = +60°C) sensor = thermistor
M/B Temp: +40.5°C (high = +52°C, hyst = +47°C) sensor = thermistor

----------------------------------------------------------

Thank you!
 
I think the code you posted is not the same when you used when you received the error. First of all, there are not 80 lines of code in your script. Second, there is no print statement for $data[1]. If you post the code you are having difficulties with, I may be able to help.

As mentioned by uida1154, your environment for your cron job may be different from the one in the shell you are using to test the script. Check your environment info to make sure your cron job has everything it needs to run correctly. You may want to to run your cron using the shell command. Something like: /bin/csh -n "/searchpath/script.pl". I am not sure about the syntax. It has been a long time since I have done this.


Michael Libeson
 
Can you die on failing opening the file? Maybe the file can not be opened from the crontab environment.
 
put system("pwd") at the head of your script to see which the crontab is using as it's source directory

--Paul

cigless ...
 
Thank you all for your replies! Starting from the top...

Michael: You're right about that it is not exactly a copy of the script. The print statement for $data[1] is just something I had for debugging. I know that is not working. I also removed many lines of commented code that was not in use any more. That's why there is not 80+ lines of code.
I've tested running the script with the command line you gave me. I get a message which I don't understand. It says:
if: Expression Syntax.

uida1154: I've tested changing the file opening line to...
open( INPUT, "/usr/bin/sensors | /bin/grep \"sensor = thermistor\" |") or die "Can't open file!";
...but no failure message is displayed in the e-mail I get from crontab. As I mentioned I can print the input line by line by using print($_);.

Paul: The source directory crontab is using is / (root). I can run the script manually from the root directory.

I hope it will make any sense to you, because to me it doesn't!
 
Try changing:

Code:
@data = split(/(.*):[ ]*(.*)°(.*)=[ ]*(.*)°(.*)=[ ]*(.*)°(.*)/);

  if($data[1] eq "CPU Temp") {
    my $sql="INSERT INTO mbtemp SET termID='1',Celsius='$data[2]'";
  } elsif ($data[1] eq "M/B Temp") {
    my $sql="INSERT INTO mbtemp SET termID='2',Celsius='$data[2]'";
  }
  if(defined $sql) {
    $dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
  }

To:

Code:
@data = split(/:/, $_);
$data[0] =~ s/^\s+//;
$data[0] =~ s/\s+$//;
$data[1] =~ s/^\s+//;
$data[1] = (split(/\s+/, $data[1])[0];
$data[1] =~ s/[^0-9\.]//g;

  if($data[0] eq "CPU Temp") {
    my $sql="INSERT INTO mbtemp SET termID='1',Celsius='$data[1]'";
  } elsif ($data[0] eq "M/B Temp") {
    my $sql="INSERT INTO mbtemp SET termID='2',Celsius='$data[1]'";
  }
  if(defined $sql) {
    $dbh->do($sql) or die "Can't execute statement $sql because: $DBI::errstr";
  }


Michael Libeson
 
Thank you all for your help! Michael's solution seems to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top