Fast boot to python script without networking?

Have some feedback, questions, suggestions, or just fancy a chat? Pop it in here.
NJS

Fast boot to python script without networking?

Post by NJS »

Hello,

I apologize in advance for my kindergarten-level working knowledge of Linux.

I am developing a wildlife camera based on Pi Zero. To conserve battery power the Pi will remain powered down until an Arduino supervisor (http://spellfoundry.com/product/sleepy-pi-2/) detects motion via a passive infrared sensor. Once motion is detected it is critical to get the Pi booted up and running my python camera script ASAP to capture the pictures.

I am not too keen on learning buildroot to try create my own custom fast-boot os image and DietPi seems to have GREAT potential as a lightweight customizable platform for my application, but it still takes ~19 seconds to boot (see systemd-analyze critical-chain output below). I would still like to cut that in half or better and would really appreciate any suggestions for how to improve it!

Removing networking is one obvious optimization as it takes 3+ seconds (even with static IP) and is not needed to take a picture. Moreover, I specifically want to keep WLAN powered down to save battery except for one specific scheduled time per day when it will wake-up and "phone home" to Google Drive. How can I do this?

What about dietpi-boot, ramdisk and/or dev-mmcblk0p1 services, which together total over 13 secs? Can these be sped up in any way? FWIW, I am already using a very fast SD card.

Anything further that can be done with overclocking (there only seems to be one profile available for Pi Zero)?

Thanks!

Code: Select all

systemd-analyze critical-chain:

graphical.target @18.846s
└─multi-user.target @18.838s
  └─getty.target @18.836s
    └─getty@tty1.service @18.828s
      └─rc-local.service @18.792s
        └─[1;31mdietpi-boot.service @13.924s +4.847s[0m
          └─[1;31mdietpi-ramdisk.service @11.396s +2.414s[0m
            └─basic.target @11.367s
              └─timers.target @11.339s
                └─systemd-tmpfiles-clean.timer @11.339s
                  └─sysinit.target @11.272s
                    └─[1;31mnetworking.service @7.974s +3.216s[0m
                      └─local-fs.target @7.718s
                        └─[1;31mboot.mount @7.525s +152ms[0m
                          └─[1;31msystemd-fsck@dev-mmcblk0p1.service @6.344s +570ms[0m
                            └─dev-mmcblk0p1.device @6.241s
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Fast boot to python script without networking?

Post by Fourdee »

NJS wrote:I am not too keen on learning buildroot to try create my own custom fast-boot os image and DietPi seems to have GREAT potential as a lightweight customizable platform for my application, but it still takes ~19 seconds to boot (see systemd-analyze critical-chain output below). I would still like to cut that in half or better and would really appreciate any suggestions for how to improve it!
Hi,

Yep its lightweight, but boot does take a little longer than non-DietPi minimal images. This is mainly for the RAMdisk and RAMlog creation, detection of hardware info of system, used in DietPi scripts (eg: network index, network enabled, network online, CPU type, CPU arch, launch ntpd, etc etc).

Static IP and disabling DietPi-RAMlog was a good start.
└─[1;31mdietpi-boot.service @13.924s +4.847s[0m
└─[1;31mdietpi-ramdisk.service @11.396s +2.414s[0m
RAMdisk is critical to DietPi operation, so that cant be disabled.

You could probably tweak/remove some commands from boot script, but either way, I cant see any worth-while reduction to boot times to warrant it.
I'am not sure what boot times your aiming for, but for me, unless its <3 seconds, you may miss the wildlife completely? :(

Have you considered leaving the system running, forcing 600MHz + powersave, power usage too much?
Once motion is detected it is critical to get the Pi booted up and running my python camera script ASAP to capture the pictures.
Hibernation/sleep may be an option to skip boot altogether, although i've never tested this and not sure RPi even supports it:
https://wiki.archlinux.org/index.php/Po ... _hibernate
Anything further that can be done with overclocking (there only seems to be one profile available for Pi Zero)?
We only add overclocking values that have been tested by us, and prove "stable". Haven't had a chance to add more yet, although, you could modify /DietPi/config.txt as needed for manual overclocking tweaks.
Moreover, I specifically want to keep WLAN powered down to save battery except for one specific scheduled time per day when it will wake-up and "phone home" to Google Drive. How can I do this?
Cron job for the scheduled time /etc/cron.daily/myscript, use dietpi-cron to change times. Without RTC/network for time sync, system time may slew and be slightly off.

WLAN, I believe bringing the adapter down, should also power it off:

Code: Select all

ifdown wlan0
Then when you need it

Code: Select all

ifup wlan0
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
NJS

Re: Fast boot to python script without networking?

Post by NJS »

Thanks for the reply. My understanding is the Pi SoC has no support for true low power mode. Standby power consumption needs to be <1mA to keep from draining the battery, which is already pretty big 5Ah 12V lead acid + 5W solar panel.

I would like to get boot under 10 seconds, but I'll take any improvement I can get relatively easily then deploy it and see how many missed triggers I have before delving into something really difficult (e.g. buildroot custom image). Since loading the networking service is over 3 seconds could I at least remove that? Or, in the alternative, would it be better to run my python script as a service earlier in the boot sequence? All it needs is the file system and camera.

BTW, the Arduino supervisor (http://spellfoundry.com/product/sleepy-pi-2/) includes HW RTC so I will only run NTP once per day when I fire up the WLAN.

Thanks again.
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Fast boot to python script without networking?

Post by Fourdee »

NJS wrote:Or, in the alternative, would it be better to run my python script as a service earlier in the boot sequence? All it needs is the file system and camera.
Yep that should do it.

The following systemD service will launch a python script /root/myscript.py before networking, and, after local FS mounted:

Code: Select all

cat << _EOF_ > /etc/systemd/system/python.service
[Unit]
Description=Python start script early
After=local-fs.target
Before=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python /root/myscript.py

[Install]
WantedBy=network.target
_EOF_
systemctl daemon-reload
systemctl enable python.service
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
NJS

Re: Fast boot to python script without networking?

Post by NJS »

Thanks. That worked (in my case I also needed to use WorkingDirectory= ).

However there is still the issue that after booting WLAN will be up and burning power. And if the WLAN hotspot is not found (which will be the normal case except for the pre-determined daily time window when the hotspot will be fired up), then I assume the networking service startup may drag on even longer while attempting to establish a connection before timing out.

I suppose I could create another service that runs after network-online.target to run "ifdown wlan0", but seems like it would be cleaner to just remove the network initialization from the boot sequence altogether and defer it to be started by my python script if/when it is appropriate. Any hints on how I can do that?

Thanks again.
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Fast boot to python script without networking?

Post by Fourdee »

NJS wrote:but seems like it would be cleaner to just remove the network initialization from the boot sequence altogether and defer it to be started by my python script if/when it is appropriate. Any hints on how I can do that?
Yep thats best route.

You should be able to control it via /etc/network/interfaces

To disable from coming up automatically during boot:
In /etc/network/interfaces , comment out:

Code: Select all

#allow-hotplug wlan0
Reload systemd

Code: Select all

systemctl daemon-reload

To enable/start:
In /etc/network/interfaces , uncomment:

Code: Select all

allow-hotplug wlan0
Reload systemd

Code: Select all

systemctl daemon-reload
Start hostapd

Code: Select all

systemctl restart hostapd

To stop:

Code: Select all

systemctl stop hostapd
#Might not be needed
ifdown wlan0
In /etc/network/interfaces , comment out:

Code: Select all

#allow-hotplug wlan0
Reload systemd

Code: Select all

systemctl daemon-reload
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
NJS

Re: Fast boot to python script without networking?

Post by NJS »

Thanks for the continued advice. I have not had a chance to try this yet. Will be somewhat tricky to modify /etc/network/interfaces on the fly from within my python script. :?

Just out of curiosity, why is 'hostapd' involved when my device is a client not an AP?
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Fast boot to python script without networking?

Post by Fourdee »

NJS wrote:Thanks for the continued advice. I have not had a chance to try this yet. Will be somewhat tricky to modify /etc/network/interfaces on the fly from within my python script. :?

Just out of curiosity, why is 'hostapd' involved when my device is a client not an AP?
You could exec shell commands from Python:
http://stackoverflow.com/questions/8922 ... -in-python

The following will replace line starting with FORCE= to FORCE=force in /etc/default/fake-hwclock:

Code: Select all

sed -i '/^FORCE=/c\FORCE=force' /etc/default/fake-hwclock
If you installed the WiFi Hotspot? This contains HostAPD. If you didnt, it shouldnt be installed. Can you please list the installed software on system so we can verify?

Code: Select all

dietpi-software list | grep ' =2 '
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
NJS

Re: Fast boot to python script without networking?

Post by NJS »

I have not installed the WiFi Hotspot. Here is what I am using (most of which I will remove from the final image once I am done developing):

id 6 | =2 | xserver: linux display system |
id 17 | =2 | git client: git clone etc |
id 23 | =2 | lxde: ultra lightweight desktop | +alsa +xserver | http://dietpi.com/phpbb/viewtopic.php?f=8&t=5#p42
id 27 | =2 | tightvnc server: desktop for remote connection | +desktop | http://dietpi.com/phpbb/viewtopic.php?f ... rt=30#p408
id 96 | =2 | samba: feature-rich file server | | http://dietpi.com/phpbb/viewtopic.php?f ... art=10#p56
id 103 | =2 | dietpi-ramlog: minimal, optimized logging | | http://dietpi.com/phpbb/viewtopic.php?f ... art=20#p68
id 104 | =2 | dropbear: ssh server | | http://dietpi.com/phpbb/viewtopic.php?f ... art=20#p68
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: Fast boot to python script without networking?

Post by Fourdee »

NJS wrote:I have not installed the WiFi Hotspot. Here is what I am using (most of which I will remove from the final image once I am done developing):

id 6 | =2 | xserver: linux display system |
id 17 | =2 | git client: git clone etc |
id 23 | =2 | lxde: ultra lightweight desktop | +alsa +xserver | http://dietpi.com/phpbb/viewtopic.php?f=8&t=5#p42
id 27 | =2 | tightvnc server: desktop for remote connection | +desktop | http://dietpi.com/phpbb/viewtopic.php?f ... rt=30#p408
id 96 | =2 | samba: feature-rich file server | | http://dietpi.com/phpbb/viewtopic.php?f ... art=10#p56
id 103 | =2 | dietpi-ramlog: minimal, optimized logging | | http://dietpi.com/phpbb/viewtopic.php?f ... art=20#p68
id 104 | =2 | dropbear: ssh server | | http://dietpi.com/phpbb/viewtopic.php?f ... art=20#p68
Thanks,

Strange, none of those software titles would be pulling in hostapd. Not sure how its been installed.
Did you install any software manually through apt-get that you can remember?
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
Post Reply