2

I'm trying to get my Raspberry Pi 2 to automatically email its IP address when it boots. I included my entire /etc/rc.local file below for reference. When I manually run the file, I get an email with the Pi's IP address, as expected. It has never worked automatically when booting. Why doesn't email script run on boot as expected and how do I fix it? I tried removing the ampersand (&) and its presence did not make a difference.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  python /home/pi/code/startup_mailer.py &
fi

exit 0
3
  • can you try it with the full path to python (/usr/bin/python)? you should also try using the full path for the hostname and printf commands. Mar 16, 2015 at 2:59
  • During boot rc.local run as root. In order to check if it could run you should try 'sudo su - -c /etc/rc.local'. Is there anything on the boot console ?
    – mpromonet
    Mar 16, 2015 at 7:33
  • Here's an idea for debugging/logging from rc.local: unix.stackexchange.com/a/189415/25985
    – goldilocks
    Mar 16, 2015 at 12:32

4 Answers 4

3

rc.local, whether you should or shouldn't be using it, will only run if:

  1. It is marked as executable (+x)
  2. You're on a system that hasn't deprecated rc.local (ie: systemctl)

Since you're dealing with IP addresses and Mail, why not load your script via /etc/network/interfaces with the following:

auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
post-up /path/to/your/script.py

Notes

5
  • How can the system determine whether or not rc.local will produce output before running it?
    – jacobq
    Apr 26, 2018 at 18:22
  • bash /etc/rc.local ? Is this what you mean?
    – earthmeLon
    Apr 26, 2018 at 18:32
  • No, I am asking about point #2 of your answer "(rc.local will only run if) There is no OUTPUT". How is that possible? Could you provide a reference supporting this claim? It sounds like a corollary to the "Halting Problem"
    – jacobq
    May 15, 2018 at 14:13
  • cron scripts can't have output. rc.local just doesn't provide output normally. I'll update the ticket, thanks for the callout.
    – earthmeLon
    May 15, 2018 at 23:01
  • Ah, OK that makes a lot more sense. The script can include commands that would generate output, but it will not go anywhere visible.
    – jacobq
    May 18, 2018 at 20:02
0

In the python program, add the location of where it should start. Add it as a command like this import os os.system("cd /path/to/script") .

0
0

Make sure ANY file referenced in your python code has an absolute path. Example:

f = open('/path/to/file.txt')

instead of:

f = open('file.txt')
0

Well, I have the same problem like you do. Then I insert this sentenses in the beginning of my script:

log_file = open ( '/my_path/mylogfile' )
log_file.write( 'Anything.' )
log_file.close()

After restart, there is something in mylogfile. In fact, my script has been executed, but exited by some exeption.

So I insert this line in the rc.local right before my script:

sleep 15

It worked!

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.