LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-19-2008, 03:44 PM   #1
beairstos
LQ Newbie
 
Registered: Jun 2008
Location: Ottawa, Ontario Canada
Posts: 4

Rep: Reputation: 0
Problem with cron


Hi everyone,

I have a small script that pings a certain address and sends me a text message on my cell if the ping goes down for too long. The script works fine, if I run it as root from the command line (and the ping I am testing is down), I get the text message.

The issue is when I put it in roots crontab. I can see in /var/log/cron that its being run. The entry I have in the cron is as follows:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/local/bin/pingtest.pl

/var/log/cron shows:

Jun 19 17:15:01 kngvsl001 crond[15884]: (root) CMD (/usr/local/bin/pingtest.pl)
Jun 19 17:20:01 kngvsl001 crond[16024]: (root) CMD (/usr/local/bin/pingtest.pl)
Jun 19 17:25:01 kngvsl001 crond[16881]: (root) CMD (/usr/local/bin/pingtest.pl)
Jun 19 17:30:01 kngvsl001 crond[17259]: (root) CMD (/usr/local/bin/pingtest.pl)

* Also I also added another job to the cron, and I'm not getting emails from it either. I've restarted the cron daemon, but it still doesn't work.

This is on a RHEL5.1 virtual box. It must be something simple, but I'm sure not seeing it.

Thanks for any suggestions you might have.

-Shawn

Last edited by beairstos; 06-19-2008 at 03:46 PM.
 
Old 06-19-2008, 04:02 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Show the pingtest.pl command.

The cron environment is more restrictive, for security. Many environment variables and typical shell variables are unset. Cron's STDOUT and STDERR also goes to email by default (otherwise, where would it go ?).
 
Old 06-19-2008, 04:15 PM   #3
beairstos
LQ Newbie
 
Registered: Jun 2008
Location: Ottawa, Ontario Canada
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Mr. C. thanks for the quick reply.

heres the basic script:

#!/usr/bin/perl

# variables
$router = "xx.xx.xx.xx";
$admin1 = "xxxxxxxxxx\@msg.telusmobility.com";
$netdev = "eth0";
$msgsub = "BELL Internet Router - Kanata - DOWN";
$msgbody = "Time to Panic!";

sub pingtest($$) {
my ($ping) = @_;
system(sprintf("ping -q -I %s -c 1 %s>/dev/null", $netdev, $ping));
$retcode = $? >> 8;
# ping returns 1 if unable to connect
return $retcode;
}

$test1 = &pingtest($router);

if ($test1 eq "1") {
$test2 = &pingtest($router); # test again in case it was just a brief blip
if ($test2 eq "1") { # ok we're down lets alert
system(sprintf("echo \"%s\"|/bin/mail -s \"%s\" %s", $msgbody,$msgsub, $admin1));
exit(1);
}
}
exit(0);
 
Old 06-19-2008, 04:35 PM   #4
Lantzvillian
Member
 
Registered: Oct 2007
Location: BC, Canada
Distribution: Fedora, Debian
Posts: 210

Rep: Reputation: 41
Simple trick, I was having issues with rsync running as a cronjob. Why no idea.. However, I had the cronjob run a script, which in turn ran my rsync script.

Try that for a test and post back.

Last edited by Lantzvillian; 06-19-2008 at 04:37 PM.
 
Old 06-19-2008, 04:49 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Try setting the MAILTO environment variable in your crontab:

Code:
MAILTO=me@example.com
and run again to see what happens.

A couple of points to note:

1) no need to use sprintf. Interpolate variables directly into your string:

Code:
system(sprintf("ping -q -I %s -c 1 %s>/dev/null", $netdev, $ping));
becomes
Code:
system("ping -q -I $netdev -c 1 $ping >/dev/null");
Do the same with your other system() call:
Code:
system(sprintf("echo \"%s\"|/bin/mail -s \"%s\" %s", $msgbody,$msgsub, $admin1));
Code:
system("echo \"$msgbody\"|/bin/mail -s \"$msgsub\" $admin1");
2) You've defined pingtest to take two arguments:
Code:
sub pingtest($$) {
but are passing only one:
Code:
&pingtest($router);
3) Calling your ping routine twice in rapid succession is not better than increasing the -C count. It is very unlikely you will catch a window of time where the first run fails, but the second run succeeds.

4) Consider using the Net::Ping::External module (http://search.cpan.org/~chorny/Net-P...12/External.pm)

Do you know about smokeping ?
 
Old 06-20-2008, 08:50 AM   #6
beairstos
LQ Newbie
 
Registered: Jun 2008
Location: Ottawa, Ontario Canada
Posts: 4

Original Poster
Rep: Reputation: 0
Well do I feel sheepish. After a good nights sleep I immediately noticed that i had neglected to name the script with the .pl at the end, but had used that in the crontab command. I'll go back under my rock now....

Thanks for all the replies, and thanks Mr. C. for your suggestions, I'll look at improving the script. I'm more of an admin than a programmer.

Thanks again,
-Shawn
 
Old 06-22-2008, 08:39 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,735

Rep: Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523Reputation: 2523
1. don't use
&pingtest($router);

the leading '&' is deprecated unless you mean pass a sub-reference

2. Change

sub pingtest($$) {
my ($ping) = @_;

to

sub pingtest
{
my ($ping) = $_[0];

scalar, not array


3. anytime you call an external prog in cron, use the full absolute path, due (as mentioned above) to the fact that the env for cron is minimal, so use

/bin/ping blah blah

4. last but not least add warnings and strict thus

#!/usr/bin/perl -w
use strict;

as the top of your code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron file 000-delay.cron (it is on my FC5) - what is the purpose? jtmoon Linux - Server 1 03-08-2007 11:15 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 01:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration