I have a shell script that periodically checks the ADSL external IP address and send it to my email if it has changed.
#! /bin/sh
NEWIP=`/usr/bin/curl ifconfig.me`
OLDIP=`cat ./current`
logger "$NEWIP ... $OLDIP"
if [ "$NEWIP" != "$OLDIP" ]; then
TIME=`/bin/date`
/usr/bin/sendEmail -v -f ip_watcher@xxxoo.com \
-s smtp.gmail.com:587 -xu ip_watcher@xxxoo.com -xp xxxxxx \
-t xxx@xxxxx.com \
-o tls=yes \
-u "$NEWIP" \
-m "$NEWIP $TIME" -a
/bin/echo "$NEWIP" > ./current
logger "IP of bjserver1 has changed ..."
else
logger "New IP is the SAME with old. not sending ..."
fi
this works perfectly when I run it from command line. but after I put it into cron, NEWIP and OLDIP are always the same. I don’t know why , can anybody help ?
What is
./current?You are not using an absolute path in the script, so the file will be wherever it is run. You should use an absolute path.
The only other significant difference between
cronand a command-line run is the user under whose account the script is executed. Make sure the account (if it’s notroot) has significant privileges to do what you’re asking it to do.Or, better yet, use an established dynamic DNS client so you don’t need to be concerned with external hostnames. You do realize you’re relying on that web site to be both honest and up, right?