WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Running Python Scripts at Startup  (Read 7585 times)

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 706
Re: Running Python Scripts at Startup
« Reply #15 on: August 18, 2022, 03:54:22 AM »
You can remove the tty from command line argument to the kernel, the console.
So you free the UART.
And remove the tty in inittab.



Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 706
Re: Running Python Scripts at Startup
« Reply #16 on: August 18, 2022, 04:38:41 AM »
You can remove the tty(serial) from command line argument to the kernel, the console.
(/boot/cmdline.txt). So you free the UART/serial port.

And remove the getty tty(serial) in /etc/inittab.
And remove the startserial.sh if you running that script, in boot script.

When i referring to tty(serial) you have to consider that the serial port have lots of different names.
console=serial0,115200
console=ttyAMA0,115200

Just have the console=tty1 in kernel command line argument(tty1 is the first text mode virtual console.), if you using raspberry pi with hdmi.

You can remove all console and using headless raspberry pi with out any console.
Just access the RPI with network ssh.
You can also do the following when using headless rpi:
Add the line to config.txt to gain little more memory, because you don't use the hdmi/gpu
 
Code: (bash) [Select]
gpu_mem=16

And the user tc have to have rights to the port, lots of linux distributions uses some type of group to the serial port, so if you add the default tc user the the correct group you don't need to use sudo for your user to use the serial port.

To easy see what group you should add the tc user to, you just list the serial device in dev.
Lets see what my arch linux pc com1 serial port.

[
Code: (bash) [Select]
root@arch-new ~]# ls -la /dev/ttyS0
crw-rw---- 1 root uucp 4, 64 18 aug 09.18 /dev/ttyS0
[root@arch-new ~]#

Ahhh it's uucp.
So if my regular user need com1 I just need to add that user to group uucp.


Happy hacking.

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #17 on: August 19, 2022, 01:37:18 AM »

And the user tc have to have rights to the port, lots of linux distributions uses some type of group to the serial port, so if you add the default tc user the the correct group you don't need to use sudo for your user to use the serial port.

To easy see what group you should add the tc user to, you just list the serial device in dev.
Lets see what my arch linux pc com1 serial port.

[
Code: (bash) [Select]
root@arch-new ~]# ls -la /dev/ttyS0
crw-rw---- 1 root uucp 4, 64 18 aug 09.18 /dev/ttyS0
[root@arch-new ~]#

Ahhh it's uucp.
So if my regular user need com1 I just need to add that user to group uucp.


Happy hacking.

Thank you, i will certainly try out several of your suggestions.

With regards to the one above, I have tried in the past, admittedly on other versions of linux, to add my user to the correct group but this still was not enough. I needed also to give read/write options to the users besides root. This worked until the next boot, which reinstated the restrictions. The second line from your code quote would always reset to: crw--w---- etc. after reboot.

Offline FlyingDutchman

  • Newbie
  • *
  • Posts: 36
Re: Running Python Scripts at Startup
« Reply #18 on: August 19, 2022, 02:39:57 AM »
Hi xuraax,

To find out why your script won't run from bootlocal.sh, you could add debug code into your python script: at important points within your script, log variable values to a log file and make sure to redirect error messages to a log file. Then after boot, you can verify why the script does not behave as expected.

Does the system log (/var/log/messages) tell you anything useful?

Alternatively, you could connect a screen to your Pi while starting up. Per default, TinyCore starts a shell for the tc user during boot and shows boot output on the terminal for that user. Although often it is a lot of output within a couple of seconds. In the past, I even had to resort to recording the output on my smartphone and then playback in slow motion to capture the error messages that I needed :-)

Offline polikuo

  • Hero Member
  • *****
  • Posts: 714
Re: Running Python Scripts at Startup
« Reply #19 on: August 19, 2022, 10:39:09 AM »
I guess your script requires user privilege
bootlocal.sh is run by root
Code: [Select]
su tc -c 'python3 yourscript'

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #20 on: August 19, 2022, 12:09:58 PM »
I guess your script requires user privilege
bootlocal.sh is run by root
Code: [Select]
su tc -c 'python3 yourscript'

Why is this better than?:
Code: [Select]
sudo python3 myscript

Offline polikuo

  • Hero Member
  • *****
  • Posts: 714
Re: Running Python Scripts at Startup
« Reply #21 on: August 19, 2022, 12:14:03 PM »
Every command in bootlocal.sh is run by root
Every command in .profile is run by tc
To run a command as tc in bootlocal.sh, you need to switch user.

Code: [Select]
sudo python3 myscript
This line will be run as root, or may not execute at all.

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #22 on: August 29, 2022, 02:11:24 PM »
Hi,

I have just discovered why my Python script does not run at all when run from bootlocal.sh. It appears that in this mode, the script starts. It then stops when it attempts to import the "serial" module issuing an error which is not reported when you connect headless but can be seen when connecting a monitor to the HDMI port.

What can I do to get the "serial" module visible ALSO at startup (from root)?

Online Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11573
Re: Running Python Scripts at Startup
« Reply #23 on: August 29, 2022, 10:53:03 PM »
Hi xuraax
... It then stops when it attempts to import the "serial" module issuing an error ...

What is the error message?
Where does the imported module come from?
Does it get download over the network? If so, it's possible the network is not up yet.

This will block (up to 60 seconds) until the network is up:
Code: [Select]
# Wait for network to come up
SEC="60"
while [ $SEC -gt 0 ]
do
        SEC=$(($SEC - 1))
        ifconfig | grep -q "Bcast:" && break
        sleep 1
done

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #24 on: August 30, 2022, 02:12:11 AM »
The problem is not network related.

When I boot up with the USB dongle removed I get the same message. When I then run "sudo python3 test.py" there is no issue which implies the python module "serial" is inside somewhere.
The message I get at startup is:

ModuleNotFoundError: No Module named 'serial'.

Python reports the script name and the line line number where my offending "import serial" resides.
Also when I remove the offending line from my script, the script runs properly at boot up.

it seems running python3 from root is different than running python3 from sudo.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 706
Re: Running Python Scripts at Startup
« Reply #25 on: August 30, 2022, 08:29:31 AM »
My suggestions is to try following.

Add the path to the working directory to sudo like this in bootlocal.sh:
Code: (bash) [Select]
sudo -D /home/tc /home/tc/test.py
So sudo change the working directory before executing the test.py script.

Add the shebang line at the first line in the test.py python file like this:
Code: (bash) [Select]
#!/usr/bin/env python3

Add the execution bit to the python file:
Code: (bash) [Select]
sudo chmod u+x /home/tc/test.py


Happy hacking :)
« Last Edit: August 30, 2022, 08:32:37 AM by patrikg »

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1238
Re: Running Python Scripts at Startup
« Reply #26 on: August 30, 2022, 08:33:22 AM »
I'm pretty sure there is no serial module included with python3.  You must have installed pyserial into a local user account which is not available when using bootlocal.   Look in /home/tc/.local/lib/......  do you see python modules?

What extensions are loaded

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #27 on: August 30, 2022, 10:22:36 AM »
 Will try but in the meantime, can you please answer the below questions?

Your suggestion 1: understood.
Your suggestion 2: What does this do? I always understood that a line started with # is ignore by the interpreter.
Your suggestion 3: Why does the execution bit need to be set given that this programs runs in the python3 interpreter?

Thanks

My suggestions is to try following.

Add the path to the working directory to sudo like this in bootlocal.sh:
Code: (bash) [Select]
sudo -D /home/tc /home/tc/test.py
So sudo change the working directory before executing the test.py script.

Add the shebang line at the first line in the test.py python file like this:
Code: (bash) [Select]
#!/usr/bin/env python3

Add the execution bit to the python file:
Code: (bash) [Select]
sudo chmod u+x /home/tc/test.py


Happy hacking :)

Offline xuraax

  • Newbie
  • *
  • Posts: 37
Re: Running Python Scripts at Startup
« Reply #28 on: August 30, 2022, 10:44:47 AM »
I'm pretty sure there is no serial module included with python3.  You must have installed pyserial into a local user account which is not available when using bootlocal.   Look in /home/tc/.local/lib/......  do you see python modules?

What extensions are loaded

It has been some time since I installed pyserial so I don't recall how I did it. Probably through pip.

Here are the contents of the directories you mentioned:

~/.local/lib/python3.8/site-packages/
pyserial-3.5.dist-info/
serial/

Both are directories.

The first looks like it contains text files.
The second contains several python scripts but none of these are called "serial.py"

For me the simplest solution would be to have all modules available even from root. i hope this is possible?

regards

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1238
Re: Running Python Scripts at Startup
« Reply #29 on: August 30, 2022, 10:52:18 AM »
So you installed pyserial using pip with the --user flag.   That will only work for user sessions.    Delete all of the python libraries from .local

Then install the extension py3.8serial.tcz