Hey,
I've a script that checks if my public IP changes in order to modify my offsite backup script. If there is a change it should send me an email. I also have a cron job that runs the script as root every 6 hours, like 0*6/***. There should be a problem with the cron job as I don't receive the email when the IP changes, but I do if I run the script directly from the terminal.
Any advice?
Bash
#!/bin/bash
ipfile='/tmp/previous_ip'
ip=$(host myip.opendns.com resolver1.opendns.com |
sed -n '/.* has address \(.*\)/ { s//\1/; p; q; }' )
if ! [[ -f $ipfile ]]; then
echo "$ip" > "$ipfile"
fi
read -r previp < "$ipfile"
if [[ $previp != "$ip" ]]; then
echo "$ip" > "$ipfile"
echo "IP change from '$previp' to '$ip'" | mailx -s "IP Change" juve
fi
Alles anzeigen