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

自古美人都是妖i 提交于 2019-12-13 06:25:37

问题


I wrote a simple python script which, 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. Here is the script

#!/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.


回答1:


I had the same problem and found It will only work on a default install only if you leave your terminal open. For whatever reason Ti did some weird changes to the Linux system that automaticity kills any background processes when your terminal is closed. Google beaglebone nohup as there are some settings you have to change to prevent the killing of your processes on exit. You will also have problems trying to get custom services started on boot up and corn does not work as you would expect it to. Welcome to the wild west of Linux




回答2:


Problem solved: I am an angstrom newb (previous linux experience is related to ubuntu exclusively), and not much experience with linux generally. So, I had been indiscriminately running opkg upgrade, following opkg update, without specifying what was being upgraded, and therefore likely introducing issues throughout my entire build. So, I started over with the 6/20 image, flashed the board, reconfigured everything, and added this to the python script (above the while loop at the top of the script):

time.sleep(45) #to avoid timing issues

i then added this in cron:

@reboot /home/root/boot_send_ip.py

And now the cron job fires on reboot and I get the IP emailed to me. YAY!

The BB google group has suggested using udhcpc to handle this type of service, which I will look into in the future, but for now this is all working via cron.



来源:https://stackoverflow.com/questions/17375663/cron-job-to-send-ip-of-beaglebone-black-at-every-reboot-to-my-email-is-not-fir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!