Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Things that normally work, but not with cron?

Things that normally work, but not with cron?
Thread Tools
Zimwy
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 6, 2003, 02:13 PM
 
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
     
chaldean oracle
Junior Member
Join Date: Nov 2003
Status: Offline
Reply With Quote
Dec 6, 2003, 02:22 PM
 
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.
     
chaldean oracle
Junior Member
Join Date: Nov 2003
Status: Offline
Reply With Quote
Dec 6, 2003, 02:23 PM
 
Also, you might look into http://dyndns.org if all you want is to keep track of your machine.
     
Zimwy  (op)
Dedicated MacNNer
Join Date: May 2002
Location: Brooklyn, NY
Status: Offline
Reply With Quote
Dec 7, 2003, 01:54 AM
 
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
     
Juggle5
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Dec 7, 2003, 03:53 AM
 
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.

     
Zim
Senior User
Join Date: Apr 2001
Location: Cary, NC
Status: Offline
Reply With Quote
Dec 7, 2003, 12:08 PM
 
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
     
si_lance
Dedicated MacNNer
Join Date: Nov 2003
Location: SoCal
Status: Offline
Reply With Quote
Dec 7, 2003, 09:57 PM
 
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 Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:31 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,