0

My script is /home/hello.py and it is using the Bottle web framework. My script needs to run after Raspberry Pi has connected to my local network. If I try to automatically run it on the boot, it doesn't work because the network connections just aren't ready yet.

I know I can I use "crontab -e" and add @reboot sleep 60 seconds -- but I don't think a sleep is too reliable.

All other "run script on boot" questions I've seen don't help because they run the scripts too early in the bootup process.

Is there anyway to check if the raspberry pi has connected and then have my python script automatically run?

4
  • What kind of interface are you using eth0, ppp? Which distribution have you installed at your Raspberry Pi? Commented Mar 23, 2015 at 6:38
  • I'm using eth0 and Raspian. Commented Mar 23, 2015 at 6:41
  • You could put your code at this folder: "/etc/network/if-up.d/". Whenever your interface is up your code will be started Commented Mar 23, 2015 at 6:49
  • As a word of warning, I have found that items in /etc/network/if-up.d/ run long before DHCP has successfully completed obtaining an IP address on my RPi (Debian Jessie). This appears to be a fairly common problem if you are depending on DHCP and not assigning a static address. Commented Oct 5, 2016 at 0:02

2 Answers 2

2

You may add your code at "/etc/network/if-up.d/upstart" just after "all_interfaces_up" like this:

all_interfaces_up() {
    python /your/code/path/codename.py
    # return true if all interfaces listed in /etc/network/interfaces as 'auto'
    # are up.  if no interfaces are found there, then "all [given] were up"

Where "/your/code/path/codename.py" is your code location

Sign up to request clarification or add additional context in comments.

3 Comments

I put this in but had no luck. Does it matter that I'm not actually logging into the raspberry and opening it's GUI? I'm accessing it remotely with ssh so I never actually open it's desktop.
The script shuld run once the interface is up. Did you restart the interface?
that doesn't work at all on Jessie... I mean nothing is getting run from that diretory
0

Don't yet have the reputation to comment on the previous solution, but instead of the suggested

all_interfaces_up() {
/your/code/path/codename.py
# return true if all interfaces listed in /etc/network/interfaces as 'auto'
# are up.  if no interfaces are found there, then "all [given] were up"

try this:

all_interfaces_up() {
python /your/code/path/codename.py
# return true if all interfaces listed in /etc/network/interfaces as 'auto'
# are up.  if no interfaces are found there, then "all [given] were up"

as you're trying to run a python script, it needs to be executed with python.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.