With a minor mod I think you can avoid a cron job
| Code: |
#!/bin/bash
# Author: Thomas Tapfumanei
# Define variables
today=`date '+%F %H:%M'`
MESSAGE="/etc/ip_address/emailmessage.txt"
email_msg="The IP address has changed the new one is: "
FILE="/etc/ip_address/old_ip"
old_ip=`awk '{print $1}' $FILE`
# Get ip address on up interface and store it in the new ip file
while true; do
new_ip=`/sbin/ifconfig ppp0 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'`
if [ "$new_ip" != "$old_ip" ]; then
# report new ip
echo "$emailmsg $new_ip as at $today" > $MESSAGE
/bin/cat $MESSAGE |/bin/mail -s " ** IP Adress has changed!! ** " user@email.com
/bin/echo $new_ip > $FILE
old_ip=$new_ip
fi
sleep 120
done
|
This will repeat 120 seconds after the previous loop finishes. You can change the delay to suit. I did something very similar in a monitoring script for my IPSec VPN. If you wanted you could pick up the current WAN IP from /var/lib/dynamicdns (eg new_ip=$( cat /var/lib/dynamicdns ). It has the advantage of making the script interface independent, but you are then reliant on the syswatch daemon sorting out the WAN IP and you do not control how often it updates. I did not bother with mine and used a similar line to Thomas.
With what I have done, if you need to end the loop you have to do something like "ps aux |grep your_script_name" and pick up the process id (pid) of your script's process, then do s "kill your_script's_process_id"