JustM3
Posts: 5
Joined: Mon May 01, 2017 3:08 pm

execute script by pi after startup

Mon May 01, 2017 3:29 pm

Hi,

I want to execute a script on my rpi3 after startup. I tried tot use /etc/rc.local and put this line between fi and exit0.

Code: Select all

sudo python /home/pi/ledmatrix/ledmatrix.py image.png & 

The .png file is a image that displays on the matrix board.

When I put the line below in rc.local, it will show a test animation on the ledmatrix board after startup.

Code: Select all

sudo python /home/pi/ledmatrix/test_2.py &
Is there a way to execute the first code line after startup?

dwhite0
Posts: 8
Joined: Sun Apr 30, 2017 3:25 pm

Re: execute script by pi after startup

Mon May 01, 2017 8:35 pm

which shell are you running? try .bashrc?

JustM3
Posts: 5
Joined: Mon May 01, 2017 3:08 pm

Re: execute script by pi after startup

Tue May 02, 2017 6:05 am

dwhite0 wrote:which shell are you running? try .bashrc?
I feel stupid, but what does a shell do/how does it work? And how can I see witch shell is running and how do I change the running shell?

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: execute script by pi after startup

Tue May 02, 2017 6:27 am

JustM3 wrote:

Code: Select all

sudo python /home/pi/ledmatrix/ledmatrix.py image.png & 

The .png file is a image that displays on the matrix board.
Where is the .png file? perhaps put the full path in, something like this:-

Code: Select all

sudo python /home/pi/ledmatrix/ledmatrix.py /home/pi/image.png & 

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: execute script by pi after startup

Tue May 02, 2017 7:04 am

don't use rc.lcoal [well you can but
a) it runs as root so no sudo needed
b) its environment is different to yours
c) a lot of things may not be setup when it runs]


use .bashrc to auto run commands [auto login also helps]
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

JustM3
Posts: 5
Joined: Mon May 01, 2017 3:08 pm

Re: execute script by pi after startup

Tue May 02, 2017 7:34 am

jahboater wrote:Where is the .png file? perhaps put the full path in, something like this:-
The image is located in the same folder as the script is placed.

When I run the script manually (when I'm located in the folder) the Terminal, it wil work. It looks like:

Code: Select all

Sudo python ledmatrix.py image.png

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: execute script by pi after startup

Tue May 02, 2017 7:52 am

Try this then:

Code: Select all

python /home/pi/ledmatrix/ledmatrix.py /home/pi/ledmatrix/image.png & 
As noted above the environment is different, and its already root.

In you case it doesn't run in your home directory and so has no idea where the "ledmatrix" folder is - so you must give it the full path.

JustM3
Posts: 5
Joined: Mon May 01, 2017 3:08 pm

Re: execute script by pi after startup

Tue May 02, 2017 5:54 pm

RaTTuS wrote: use .bashrc to auto run commands [auto login also helps]
Thanks for the respons! It helped me a bit further.
I tried to use .bashrc and it works as soon as I asses the terminal (by ssh). But I need the script to run automatically (almost) as soon as I power-up my raspberry pi. The place where I want to use it doesn't have network acces.

I tried to auto login, but somehow I can't get it fixed with Jessie.

JustM3
Posts: 5
Joined: Mon May 01, 2017 3:08 pm

Re: execute script by pi after startup

Tue May 02, 2017 6:13 pm

jahboater wrote:Try this then:
CODE: SELECT ALL
python /home/pi/ledmatrix/ledmatrix.py /home/pi/ledmatrix/image.png &
As noted above the environment is different, and its already root.

In you case it doesn't run in your home directory and so has no idea where the "ledmatrix" folder is - so you must give it the full path.
Thanks! After I used the full path in /etc/rc./local it will run automatically after booting.

I still want to see/learn if I can get the .bashrc running the same way (if it is posable), because you can quit it easy with "Ctrl+C" as soon as I connect to my pi.

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: execute script by pi after startup

Wed May 03, 2017 7:14 am

edit .bashrc and at the end put

Code: Select all

if [ $(tty) == /dev/tty1 ]; then
   ./script
fi
where ./script is the script in the home directory [just put the commands direct in there]

this will then auto run whenever you log in and if it is on the first terminal then it will run
it will not run if you are on other terminals or via ssh

then using raspi-config
set your machine to auto login a terminal session
...

the better way to do it is via the systemd system and start a service on the terminal you want with input from that session with the user you want
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Troubleshooting”