WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Run python script on boot  (Read 2971 times)

Offline Knonf

  • Newbie
  • *
  • Posts: 3
Run python script on boot
« on: May 26, 2020, 10:31:16 AM »
Hi,

sorry, i know that this should be a basic task and there are dozends of similar threads on the internet, but i can't get it to work for some reason.

I have a pi zero w with piCore 9.0.3 . Setup is done according to the README. I want to run a simple python script with a paho-mqtt client. The script itself runs perfectly when executed manually via ssh (no errors; values are received by mqtt broker). But it doesn't runns on boot.

My /opt/bootlocal.sh looks like this:

Code: [Select]

/usr/sbin/startserialtty &

# Set CPU frequency governor to ondemand (default is performance)
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

# Load modules
/sbin/modprobe i2c-dev

# Start openssh daemon
/usr/local/etc/init.d/openssh start

# ------ Put other system startup commands below this line

/usr/local/bin/wifi.sh -a 2>&1 > /tmp/wifi.log

python3.6 /home/tc/app/mqtt-client.py 2>&1 > /tmp/python.log

iperf3 -s &



The python.log file is just there for testing. The script is not running with or without that log statement. If the statemant is there, the log file is created during boot. But it is empty. 
It's not even the final mqtt script. There are no external sensors, etc. . Just a mqtt client that reports hard-coded values.

The iperf3 Server is also there for testing. It runns without any issue on boot.

I also tried another python script (a simple flask app). Same issue.


It's the first time that i use tinycore/picore. Normally i would use systemd for such a task  ;)

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: Run python script on boot
« Reply #1 on: May 26, 2020, 10:47:58 AM »
Hi Knonf!

bootlocal.sh is executed with the root permissions, maybe this is the problem?
And python3.6.tcz is in onboot.lst already?
EDIT: and maybe add some delay after wifi.sh.
« Last Edit: May 26, 2020, 10:58:22 AM by jazzbiker »

Offline Knonf

  • Newbie
  • *
  • Posts: 3
Re: Run python script on boot
« Reply #2 on: May 26, 2020, 01:15:01 PM »
Thank you for your quick answer.

Quote
bootlocal.sh is executed with the root permissions, maybe this is the problem?

If i run it manually with

Code: [Select]
sudo python3.6 ....
it still works. So root should be no problem !?

Quote
And python3.6.tcz is in onboot.lst already?

Yes it is.

Quote
and maybe add some delay after wifi.sh.

Good idea. I added the following line to bootloacal.sh after wifi.sh:

Code: [Select]
sleep 2m

But... still nothing.  :-\  If i remove the whole python call / sleep and start iperf right after wifi.sh, iperf still works... So the wifi setup is fast enough and requires no sleep?

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: Run python script on boot
« Reply #3 on: May 26, 2020, 03:25:43 PM »
HI, Knonf!

Now the only thought related Your problem is that when bootlocal.sh is executed Your $HOME don't exists yet, so python has nothing to execute. Now I will look into the system start scripts, while You can try to move Your python call to ~/.profile.

EDIT: No, restore is called just right before bootsync.sh. Unless some races, $HOME must have existed.
Try python full path, just like for wifi.sh and previous executables.
Code: [Select]
/usr/local/bin/python3.6 ...
« Last Edit: May 26, 2020, 03:56:31 PM by jazzbiker »

Offline Knonf

  • Newbie
  • *
  • Posts: 3
Re: Run python script on boot
« Reply #4 on: May 27, 2020, 10:09:42 AM »
Hi jazzbiker,

Quote
No, restore is called just right before bootsync.sh. Unless some races, $HOME must have existed.

Sounds right. I put the code in different places now (like /mnt/mmcblk0p2/app ... - makes that one even sense?) and nothing changed. Also the full path to python3.6 changed nothing.

I also took a second pi ("normal" 1.2 B+) with a new sd card, picore 9.0 and an ethernet connection for sanity checking. Still got the same issue.

But then i wrote a absolute minimal test script without any pip (!) module imports. It's just a print every second in an endless loop. And that one works. At least i see the python process running in top. The modules are persistent (tested with manual execution after reboot). But is it possible, that the modules / the folder are / is not there yet during the execution of the bootlocal script?


I followed that answer for pip installation: https://unix.stackexchange.com/questions/569703/does-tinycores-picores-python-limits-prevent-python-library-installation-for

The modules are in ~/.local/lib/python3.6/site-packages/
Not in usr/local/lib/python3.6/site-packages/

EDIT: Yes, just double checkt it. The scripts are not executed during boot as soon as I import a module which was installed via pip.

Strange, that the log file is empty. I imagined that i would see errors (like import errors) there. Thats the reason why i not looked into this earlier.


« Last Edit: May 27, 2020, 10:21:14 AM by Knonf »

Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: Run python script on boot
« Reply #5 on: May 27, 2020, 10:21:45 AM »
Hi, Knonf!

So haven't You tried to call from ~/.profile?