Alan Scarpa
Alan Scarpa

Reputation: 3560

How to automatically run Python script AFTER Raspberry Pi fully boots?

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?

Upvotes: 0

Views: 7064

Answers (2)

dgsleeps
dgsleeps

Reputation: 700

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

Upvotes: 2

Geert van Dijk
Geert van Dijk

Reputation: 136

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.

Upvotes: 0

Related Questions