Solved Problem with Bash Script

October 18, 2015 at 05:15:04
Specs: Windows 7
OS = FreeBSD v10.1
Shell = Bash

Problem:
I'm trying to write a script that will check my external IP and if it changes, email me the changed IP. The script works perfectly from the command line when I run it manually but when I make it a cronjob, the email arrives without the IP. I'm stumped. Can anybody (Nails) tell me what I'm doing wrong.

#!/usr/local/bin/bash

# Cronjob: Every night at 03:00
# *  3  *  *  *  ~/bin/checkip

# get today's date
D=$(date)

# get present IP
IP_NOW=$(dig +short myip.opendns.com @resolver1.opendns.com)

# read past IP into variable for comparison purposes
IP_PAST=$(<~/bin/IP.past)

# compare present IP to yesterday's to see if it's the same
if [ "$IP_NOW" != "$IP_PAST" ]; then

    # update IP_PAST with new IP address
    echo "$IP_NOW" > ~/bin/IP.past

    # update log file with date for change tracking reasons
    echo "$D" >> ~/bin/IP.change

    #email new IP to me
    echo "External IP has changed to: $IP_NOW" | mail -s "External IP Changed" myemail@home.com

else
    # update logfile
    echo "$D: IP has not changed" >> ~/bin/IP.log
fi

exit

It matters not how straight the gate,
How charged with punishments the scroll,
I am the master of my fate;
I am the captain of my soul.

***William Henley***

message edited by Curt R


See More: Problem with Bash Script

Report •

#1
October 18, 2015 at 06:50:42
✔ Best Answer
What directory is "dig" located in? Remember that the $PATH variable in the cron environment will not be the same as in your interactive environment.

Report •

#2
October 18, 2015 at 08:07:14
*Forehead smack*

Thanks ijack for pointing me in the right direction. I was able to get it working. :)

I ran into this same problem about 2 years ago and farted around for hours before breaking down and asking someone for help. You'd think a guy would remember but if you only use something every once in a blue moon.......

I first tried adding the absolute path to "dig" (/usr/loca/bin) in /etc/crontab but that didn't work. I'm thinking that's because that is the settings for root. I don't want to run the script as root, I want to run it as the user. I cheated and added $PATH out of /etc/crontab to my script under the shebang and it works. But I'm not sure that's the best way to do it.

Is it? Or should I be adding that path elsewhere?

It matters not how straight the gate,
How charged with punishments the scroll,
I am the master of my fate;
I am the captain of my soul.

***William Henley***


Report •

#3
October 18, 2015 at 09:41:11
Curt:

It's not cheating to add $PATH or any other required environmental shell variable to a script - it's a best method.

Also, it's best not to rely on the location of your $HOME variable as a requirement for your script to execute. What happens if some other user other than yourself tries to run your script?


Report •

Related Solutions

#4
October 19, 2015 at 04:07:18
Good to know nails I figured it made the most sense to add the path inside the script but wanted to be sure there wasn't a better place to do so.

As for others running it, well this is my PC at home and nobody else is going to ever be logging into it.

I'm too cheap to pay for dynamic DNS or a static IP and since "dig" easily finds my external IP I can now send an email to my work if it changes. No more calling my wife (if she's even home) and asking her to check so I can remote in.

It matters not how straight the gate,
How charged with punishments the scroll,
I am the master of my fate;
I am the captain of my soul.

***William Henley***


Report •

Ask Question