|
|
Things that normally work, but not with cron?
|
|
|
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
Hi,
I have a script that runs from Cron, when no user is logged into the system. Here's the script:
Code:
#!/usr/bin/perl
my $email_addr = '[email protected]';
$currentIP = `echo -n ipconfig getifaddr en0`;
if ($currentIP != "128.148.xxx.xxx"){
#We've changed IPs! Send the email!
&SendMail
}
sub SendMail {
# Run or die quietly
open( MAIL, "|/usr/sbin/sendmail -oi -t");
print MAIL <<"EOF";
From: TheIMac\@oak.myvnc.com
To: $email_addr
Subject: New IP Address!
The new IP address of the imac is $currentIP
EOF
close(MAIL);
}
So, if I test this out manually by running, it works beautifully. But, if I run it from cron with no user logged in, the ipconfig getifaddr en0 returns nothing. Any advice?
Thanks!
gabe
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Nov 2003
Status:
Offline
|
|
Troubleshooting technique:
1. Hmmm... there's a problem with running ipconfig in cron, but not usually.
2. What causes problems running commands in different environments? Usually, an environment variable.
3. What environment variable could affect whether a command is run? A: the $PATH.
4. I wonder whether ipconfig might not be in the $PATH in the crontab?
5. which ipconfig
/usr/sbin/ipconfig
6. Ahh, so it's probably not in the $PATH in crontab.
7. Solution: change ipconfig to /usr/sbin/ipconfig in the script and you'll be all set.
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Nov 2003
Status:
Offline
|
|
Also, you might look into http://dyndns.org if all you want is to keep track of your machine.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status:
Offline
|
|
Originally posted by chaldean oracle:
Troubleshooting technique:
1. Hmmm... there's a problem with running ipconfig in cron, but not usually.
2. What causes problems running commands in different environments? Usually, an environment variable.
3. What environment variable could affect whether a command is run? A: the $PATH.
4. I wonder whether ipconfig might not be in the $PATH in the crontab?
5. which ipconfig
/usr/sbin/ipconfig
6. Ahh, so it's probably not in the $PATH in crontab.
7. Solution: change ipconfig to /usr/sbin/ipconfig in the script and you'll be all set.
Thank you for your troubleshooting techniques. However, I would assume that if the command wasn't found, that perl would spit on an error, which would then get emailed to me since it's running as a cron job.
gabe
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Nov 2001
Location: Seattle
Status:
Offline
|
|
This probably won't fix anything, but I do have question:
Since $currentIP is a string variable, do you have to use 'ne' (string compare) rather than '!=' (numerical compare) in the if statement:
if ($currentIP != "128.148.xxx.xxx"){
If I'm right, I don't understand how this would ever work, even when you were logged in. If I'm wrong, could you enlighten me? I'm always willing to learn.
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status:
Offline
|
|
hmm, well, I've had a script running cron for 2+ years now, that updates my IP to an FTP location just as a way way backup, in case all else goes wrong with my dyndns setup... it is
Code:
#!/usr/bin/perl -w
use strict;
use Cwd;
use Net::FTP;
my $ipfile = "/tmp/ip.html";
my $ip = "0.0.0.0";
my $ftp;
my $myip;
my $date = substr(scalar localtime,4,15);
open(IPFILE ,">$ipfile");
$ip = `/sbin/ifconfig -a | grep broadcast`;
($myip) = ($ip =~ m/inet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask/);
print IPFILE "<html><head><title>IP Addr</title></head>
<body>
<p>$myip</p>
<p>updated: $date</p>
</body></html>\n";
close(IPFILE);
$ftp = Net::FTP->new("foo.bar.com",
Timeout => 30,
Debug => 1,
Passive => 1) or die "Can't connect $@\n";
$ftp->login("user","pass");
$ftp->put($ipfile,"ip.html") or die "Can't send ip.html $!\n";
$ftp->quit();
unlink("$ipfile");
cron entry
Code:
59 * * * * /Users/zimmy/bin/ip.pl > /dev/null 2>&1
I use ifconfig, not iconfig.. shrug.
Cheers,
Mike
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Nov 2003
Location: SoCal
Status:
Offline
|
|
as a test trying making a wrapper shell script like:
#!/bin/bash
# this script will fire off script.pl from cron
/path/to/perl/script
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|