djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Launch python from rc.local

Wed Nov 25, 2020 9:41 pm

Hello, I am trying to run a python script I found to launch a display and display the local ip at boot. I have the prereusites installed, and can run the script successfully from the terminal.

I have tried modifying my rc.local to run it.

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address

python /home/pi/Downloads/I2C_SSD1306_ipaddr.py

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

raspivid -t 0 -w 1280 -h 960 -fps 30 -b 2000000 -awb auto -n  -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=0.0.0.0 port=8554


exit 0
I made it executable (I think it already was, because my raspivid commands work, or at least they did. ) with sudo chmod +x
I have tried launching it from the beginning and end, with and without sudo.

Why can't it activate the display? Do I maybe need a delay?

Also, is there an alternative way to launch it at boot?

klricks
Posts: 7448
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Launch python from rc.local

Wed Nov 25, 2020 10:22 pm

Desktop GUI apps won't easily work from rc.local as it attempts to run your command before the Desktop is ready.
Much easier to use autostart instead.

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Re: Launch python from rc.local

Wed Nov 25, 2020 10:46 pm

klricks wrote:
Wed Nov 25, 2020 10:22 pm
Desktop GUI apps won't easily work from rc.local as it attempts to run your command before the Desktop is ready.
Much easier to use autostart instead.

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart

Thanks!
It has no gui, it just prints the ip address. It's one of those mini 2 color oleds. I will try take a look at using autostart.
I will update if it works or not.

djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Re: Launch python from rc.local

Wed Nov 25, 2020 11:19 pm

klricks wrote:
Wed Nov 25, 2020 10:22 pm
Desktop GUI apps won't easily work from rc.local as it attempts to run your command before the Desktop is ready.
Much easier to use autostart instead.

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
I looked it up, and found a guide for autostart that had me make a .desktop file in a different location

/home/pi/.config/autostart

It worked though. Thanks a lot for the help.

klricks
Posts: 7448
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Launch python from rc.local

Thu Nov 26, 2020 12:05 am

djhnsn wrote:
Wed Nov 25, 2020 11:19 pm
klricks wrote:
Wed Nov 25, 2020 10:22 pm
Desktop GUI apps won't easily work from rc.local as it attempts to run your command before the Desktop is ready.
Much easier to use autostart instead.

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
I looked it up, and found a guide for autostart that had me make a .desktop file in a different location

/home/pi/.config/autostart

It worked though. Thanks a lot for the help.
Great you got it going using one of several alternate methods.
The method I mentioned, (global autostart) does not use a .desktop file.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Re: Launch python from rc.local

Thu Nov 26, 2020 3:49 am

klricks wrote:
Thu Nov 26, 2020 12:05 am
djhnsn wrote:
Wed Nov 25, 2020 11:19 pm
klricks wrote:
Wed Nov 25, 2020 10:22 pm
Desktop GUI apps won't easily work from rc.local as it attempts to run your command before the Desktop is ready.
Much easier to use autostart instead.

Code: Select all

/etc/xdg/lxsession/LXDE-pi/autostart
I looked it up, and found a guide for autostart that had me make a .desktop file in a different location

/home/pi/.config/autostart

It worked though. Thanks a lot for the help.
Great you got it going using one of several alternate methods.
The method I mentioned, (global autostart) does not use a .desktop file.
When I looked inside the global autostart, I wasn't really sure what to do. I saw a few things loading with @ before them, but wasn't sure if that would apply to a directory as well. When I looked for it, I found the method I ended up using.

It would be to my benefit to learn the global method, so I will look into it the next time I set one up. I haven't designed and printed a case yet, so I want to get that sorted out.

I have 2 more to go. I am making little cameras on pi 0 w to feed video to a jetson nano base station. I don't have much experience with iot devices, and this is my first venture. I have that part working, but I figure I should take advantage of the gpio.

Also, I am finding a lot of the info out there to be very outdated, but that is how these things go.

Anyway, tldr= Thanks

hippy
Posts: 8958
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Launch python from rc.local

Thu Nov 26, 2020 12:00 pm

djhnsn wrote:
Wed Nov 25, 2020 9:41 pm
I have tried modifying my rc.local to run it.
That should work.

You can add the following early in /etc/rc.local to create a log file which allows what's going on within rc.local to be monitored ...

Code: Select all

exec > /tmp/rc.local.log 2>&1; set -x
Then, when rebooted, to see what happened ...

Code: Select all

cat /tmp/rc.local.log
My guess would be that you are trying to run a Python 3 program with the "python" command which treats it as Python 2 code, or there's some issue within your actual Python code.

klricks
Posts: 7448
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Launch python from rc.local

Thu Nov 26, 2020 3:01 pm

djhnsn wrote:
Thu Nov 26, 2020 3:49 am
...
When I looked inside the global autostart, I wasn't really sure what to do. I saw a few things loading with @ before them, but wasn't sure if that would apply to a directory as well. When I looked for it, I found the method I ended up using.

It would be to my benefit to learn the global method, so I will look into it the next time I set one up. I haven't designed and printed a case yet, so I want to get that sorted out.

I have 2 more to go. I am making little cameras on pi 0 w to feed video to a jetson nano base station. I don't have much experience with iot devices, and this is my first venture. I have that part working, but I figure I should take advantage of the gpio.

Also, I am finding a lot of the info out there to be very outdated, but that is how these things go.

Anyway, tldr= Thanks
You simply add your command below what is already there. There are some limitations however... If you have complex or multiple commands, it is best to put them in a bash script then call the bash script from autostart.
The @ is optional. When @ is present, when there is an error, the system will retry the command up to 4 more times before giving up.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Re: Launch python from rc.local

Fri Nov 27, 2020 1:25 am

hippy wrote:
Thu Nov 26, 2020 12:00 pm
djhnsn wrote:
Wed Nov 25, 2020 9:41 pm
I have tried modifying my rc.local to run it.
That should work.

You can add the following early in /etc/rc.local to create a log file which allows what's going on within rc.local to be monitored ...

Code: Select all

exec > /tmp/rc.local.log 2>&1; set -x
Then, when rebooted, to see what happened ...

Code: Select all

cat /tmp/rc.local.log
My guess would be that you are trying to run a Python 3 program with the "python" command which treats it as Python 2 code, or there's some issue within your actual Python code.
It's old code, it launches with python. python3 brings the errors in. I will absolutely generate a logfile. Thanks for that!

klricks wrote:
You simply add your command below what is already there. There are some limitations however... If you have complex or multiple commands, it is best to put them in a bash script then call the bash script from autostart.
The @ is optional. When @ is present, when there is an error, the system will retry the command up to 4 more times before giving up.
Thanks for this, I will let you know how it goes in few days.

djhnsn
Posts: 13
Joined: Mon Nov 23, 2020 8:45 pm

Re: Launch python from rc.local

Sun Nov 29, 2020 11:06 pm

klricks wrote:
Thu Nov 26, 2020 3:01 pm
djhnsn wrote:
Thu Nov 26, 2020 3:49 am
...
When I looked inside the global autostart, I wasn't really sure what to do. I saw a few things loading with @ before them, but wasn't sure if that would apply to a directory as well. When I looked for it, I found the method I ended up using.

It would be to my benefit to learn the global method, so I will look into it the next time I set one up. I haven't designed and printed a case yet, so I want to get that sorted out.

I have 2 more to go. I am making little cameras on pi 0 w to feed video to a jetson nano base station. I don't have much experience with iot devices, and this is my first venture. I have that part working, but I figure I should take advantage of the gpio.

Also, I am finding a lot of the info out there to be very outdated, but that is how these things go.

Anyway, tldr= Thanks
You simply add your command below what is already there. There are some limitations however... If you have complex or multiple commands, it is best to put them in a bash script then call the bash script from autostart.
The @ is optional. When @ is present, when there is an error, the system will retry the command up to 4 more times before giving up.
This worked perfectly! Very simple and effective. Thanks again!

Return to “Troubleshooting”