Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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 ?).
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);
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.