cron job to send ip of BeagleBone Black (at every reboot) to my email is not firing

Here is a python scrip that I am using that, when executed, gets my BeagleBone Black’s ip address and sends it to my email address, preventing me from having to plug the board into my laptop, get the ip, unplug it, and then remotely SSH in. See below:

#!/usr/bin/python

"""Find own ip address and email it."""

import socket
import datetime, time
import sys

failed = 1
while(failed):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8",80))
        my_ip = s.getsockname()[0]
        s.close()
        failed = 0
    except SocketError:
        print sys.exc_info()[0]
    except:
        print error
    time.sleep(5)

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

msg = MIMEText("cta_BBB_1 ip address: %s" % my_ip)

me = '<outgoing message email address>'
you = '<incoming message receiver>'

msg['Subject'] = 'cta_BBB_1 ip address at ' + str(datetime.datetime.now())
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('<server_name>', <server_port))
s.login('<login>', '<password>')
s.sendmail(me, [you], msg.as_string())
s.quit()

The script works perfectly if I just run it on the BBB. I then made the script executable and wrote a cron job to fire it, which looks like this (ignore the second line, that deals with resetting the date/time on the BBB):

@reboot /usr/bin/python /home/root/cta_stuff/cta_boot_send_ip.py
30 * * * *    /usr/bin/ntpdate-sync silent

Which does show up if I run crontab -l.

So, when I hard reboot (via the reset button), reboot via ssh, or halt the board and then turn it back on, the cron job does not fire the script (i.e. no email is received with the ip address). I have checked formatting, permissions, etc., on everything I can think of involved in this task on the BBB. If anyone has any idea why it is not working, I would really appreciate some help, as I am totally stumped.

Also, I am currently using the BBB-eMMC-flasher-2013.06.06.img image for Angstrom.

Is it possible that cron could be initiating your script before network services are up? Have you checked the journal? (journalctl -b)

You might have more control over insuring that the script launches after network services are available if you set this up as a service under systemd with an appropriate target.

If the BBB is installed on a network that you control, another tact (the one I use!) is to set up the router’s DHCP reservation table so that when a DHCP request comes in from your BBB’s mac address, it will always get the same IP address. Note that if you use your BBB in wireless mode at times and wired modes at other times, you will need to make two reservations since the mac address is different in both cases.

Don

With Debian, I would run this script from init.d, and have it execute after network was up( this can be done with init scripts ). With Angstrom and systemmd, I am not sure what all is available, but would have to assume there would be a similar option.

However, the way this seems to be playing out on your system almost seems to indicate a permissions issue.

I would not use cron for this. What you care about is not what time or date it is, but that the BB has just rebooted. I would make this part of your bootup, and as already mentioned by Don Miller, make sure that the network is up first.

Neither cron or init is the proper place for this code. What you care
about is getting an update when the IP address is assigned or chagned
via DHCP. There are typically "hook" scripts run by most dhcp clients
that will let you send your e-mail (or even better, register a domain
name with your dynamic DNS server) when you get a new IP.

With Debian and the ISC dhcp client, you'd just drop a script into:

/etc/dhcp/dhclient-exit-hooks.d/

See "man dhclient-script" for details.

There should be a similar feature for the dhcp client in Angstrom if
it's not running the ISC client.

...or just setup your DHCP server to register clients automatically
with a dynamic zone in your DNS server. Then the client doesn't have
to do anything.

- --
Charles Steinkuehler
charles@steinkuehler.net

hi all,

thanks for all the info. i do not see anything in the journal that suggests that the script fired before the network connection was established. nonetheless, ill give the systemd services thing a shot and report back. what is odd is a friend of mine is using the exact same script, and firing it as a cron job, and has no issues, though we are both on different images, so perhaps something changed since the June 6th emmc flasher image.

casey

Casey,

I know that I’m the one that mentioned systemd in the first place, but Charles’ idea sounds pretty good to me.

Angstrom appears to use udhcpc as it’s DHCP client, and if no -s or --script=file option is placed on its command line, the busybox docs say that it defaults to looking for a script named:

/usr/share/udhcpc/default.script

It may be worth placing your script in that location to see what happens.

Don

okay, so i’ve solved the issue, but not the way that any of you suggested. though im going to explore the udhcpc method later, while talking to a colleague it was pointed out to me that one should not be doing opkg upgrade, following opkg update, without parameters specifying what is to be upgraded (im an angstrom newb, and this has so far been way different than my moderate ubuntu experience provided). so, i thought i would start by flashing the board with the most recent image, reinstall and configure everything, and try the method that works for my colleague. so, here are the slight changes i made (all of which work) following re-doing everything:

in the python script:

added time.sleep(45) prior to the while loop in order to avoid any potential timing issues during reboot

in the crontab i now have this:

30 * * * * /usr/bin/ntpdate-sync silent
@reboot /home/root/boot_send_ip.py

so, this totally works now, but since it has been suggested that it would be better to use udhcpc, i will look into that route later. in the meantime, though, this is now working as a cron job.

thanks for all of your help!

casey

Hello, I would like to know if this script would work only to get a local (LAN) ip address, or a public (Internet) address? I need to connect to my BeagleBone using ssh over the internet, and I don’t want to set up a dyn dns.

Best regards.