1

I am trying to have my pi check my outside ip address every 5 minutes then compare it to the current ip stored in a text file. If the ip is different then I want it to update the text file, and email me. If the ip is the same then I want it to do nothing.

I have tried doing this with using crontab -e. I have also tried Sudo crontab -e, but have had no luck. Here is the command I am using in crontab.

0,15,30,45 * * * * pi /home/pi/scripts/updateip.sh &>/dev/null

The script I am running is located under the user name Pi (which I will be changing after I figure out how to do this), and it looks like this.

GNU nano 2.2.6            File: updateip.sh                                
#!/bin/sh
SUBJ="ip address has changed"
EMAIL="[email protected]"
ip1=""
ip2=""
read ip1 < ip.txt
ip2=$(wget -qO- ifconfig.me/ip)
if [ "$ip1" = "$ip2" ]
then
  exit
else
  echo "$ip2" > ip.txt
  echo "$ip2" | mail -s $SUBJ $EMAIL
 exit
fi

I am able to execute the script manually, but scheduling is another issue all together. I can confirm that I am receiving emails from the Pi. I have tried several other solutions found on this site, to no avail. thanks in advanced for your help.

EDIT I should note the issue appears to be the scheduled task just isn't running.

EDIT 2

The way I went about this was making it harder on me. I redid the whole solution. I followed a youtube video, and now my pi is working as desired. here is the link.Outgoing IP email tutorial

2
  • Is the script executable, i.e. have you done a chmod +x updateip.sh ?
    – joan
    Commented Jun 17, 2015 at 19:30
  • @joan I have made it executable.
    – alphamalle
    Commented Jun 17, 2015 at 20:45

4 Answers 4

4

Try to use absolute paths inside your bash script.

read ip1 < ip.txt

and

echo "$ip2" > ip.txt

Change ip.txt to /home/pi/ip.txt or something like that.

The other thing would be the crontab line itself. Why do you have the "pi" there? It goes:

minute hour day_of_month month day_of_week   command

If you want it to execute with the pi user, just edit the crontab of the pi user:

crontab -e.

Calling this with sudo edits the crontab of the root user.

sudo crontab -e

Finally, when a crontab runs it logs in syslog and if there's an issue it sends an internal mail. To read the entries, just write mail in the console.

Hope this helps.

4
  • in regards to the command" section of the con file should I put "sh"then the command or just the command itself (being the file path to the command)
    – alphamalle
    Commented Jun 17, 2015 at 16:08
  • Just the command.
    – dekomote
    Commented Jun 17, 2015 at 20:44
  • I did as you suggested, and I'm closer, but still not quite there. The script still works when ran on it's own, and cron now emails me at the time it should, but the emails don't hold the data I need. It gives me two emails. one which says Delivery to the following recipient failed permanently: pi@raspberrypi , and another that gives an error about dns. DNS Error: Address resolution of raspberrypi. failed: Domain name not found
    – alphamalle
    Commented Jun 17, 2015 at 21:52
  • Try it with a gmail account.
    – dekomote
    Commented Jun 23, 2015 at 9:01
2

EDIT I should note the issue appears to be the scheduled task just isn't running.

Take a look at your cron entry:

0,15,30,45 * * * * pi /home/pi/scripts/updateip.sh &>/dev/null

Is there a command called pi on your system? What does it do?

I'm guessing there isn't any such command, or if there is you're not intending on running one, but are instead trying to invoke the script as the 'pi' user. This isn't a valid syntax, change this:

pi /home/pi/scripts/updateip.sh

To just this:

/home/pi/scripts/updateip.sh 

Additionally when running something from cron it helps to pipe the command output to a file instead of '/dev/null' when trying to debug issues. Change this:

&>/dev/null

To this:

&> /tmp/updateip.debug.log

Then you can view the script output the next time cron attempts to run that task.

3
  • Also, if you want you can use shorthand on the cron scheduling. Instead of specifying 0,15,30,45 you can use */15 to run it every fifteen minutes.
    – Gene
    Commented Jun 17, 2015 at 19:37
  • I did this, and now even though cron runs I get an email saying DNS Error: Address resolution of raspberrypi. failed: Domain name not found. As opposed to the external ip. I know the script is correct though because when ran manually it works perfectly.
    – alphamalle
    Commented Jun 17, 2015 at 21:55
  • Change #!/bin/sh to #!/bin/sh -x ins the script and wait for cron to run it again. It'll show the commands it's running and help you narrow down where the problem is in the script.
    – Gene
    Commented Jun 17, 2015 at 22:01
0

I'm not sure if this would give quite the symptoms you appear to have. It may be because your current directory is not where you previously stored the ip file.

Try adding the following line as the second line of updateip.sh.

cd /home/pi/scripts

3
  • Should I delete #!/bin/sh then add it, or just add it above that?
    – alphamalle
    Commented Jun 17, 2015 at 13:03
  • Add it after the first line (#!/bin/sh).
    – joan
    Commented Jun 17, 2015 at 16:50
  • unfortunately this didn't work.
    – alphamalle
    Commented Jun 17, 2015 at 21:53
-1

The way I went about this was making it harder on me. I redid the whole solution using python. I followed a youtube video, and now my pi is working as desired. here is the link.Outgoing IP email tutorial

1
  • Could you please outline the basic steps in your answer. Otherwise if the link breaks in the future then your answer becomes useless.
    – Darth Vader
    Commented Nov 28, 2017 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.