Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Run program at Boot

Fri Dec 13, 2019 8:16 pm

Trying to set up to run my program at boot.
Added this to the "/etc/rc.local" just before the exit 0 at the end.

Code: Select all

sudo python3 /home/pi/max7219/examples/VPsong-19.py & > /home/pi/Desktop/log.txt 2>%1
But it does not run at start up, the log.txt appears on the Desktop but is empty.

How do I get it to run.

pcmanbob
Posts: 7965
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Run program at Boot

Fri Dec 13, 2019 8:41 pm

Hi

So first question is what are you expecting the program to do ?

Are you expecting any output on the screen.

If you want to run it the background then you can use rc.local,

So do don't need the sudo command because rc.local is already running as root.
But you do need to move the & to the end of the line..

So try this

Code: Select all

 python3 /home/pi/max7219/examples/VPsong-19.py >> /home/pi/Desktop/log.txt 2>&1 &
 
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Fri Dec 13, 2019 10:25 pm

Hi,

Many thanks for your reply.
With your code, something is happening now, the log text state error "no such file or directory", the files are on a separate USB stick and the program runs OK from the Python editor. The directory for the usb is "media/pi/storage/xxxx.txt".

Can it not access this at that stage?

The program use tinker and displays a window with text, push buttons to GPIO pins and seven segment led.

Thanks David

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

Re: Run program at Boot

Fri Dec 13, 2019 10:38 pm

Daveolw1 wrote:
Fri Dec 13, 2019 10:25 pm
Hi,

Many thanks for your reply.
With your code, something is happening now, the log text state error "no such file or directory", the files are on a separate USB stick and the program runs OK from the Python editor. The directory for the usb is "media/pi/storage/xxxx.txt".

Can it not access this at that stage?

The program use tinker and displays a window with text, push buttons to GPIO pins and seven segment led.

Thanks David
Since your code requires GUI (tkinter) you can not use rc.local to start it as the code is executed before the GUI has started.
Use autostart instead:

Code: Select all

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

User avatar
thagrol
Posts: 2229
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Run program at Boot

Sat Dec 14, 2019 2:13 pm

Daveolw1 wrote:
Fri Dec 13, 2019 10:25 pm
With your code, something is happening now, the log text state error "no such file or directory", the files are on a separate USB stick and the program runs OK from the Python editor. The directory for the usb is "media/pi/storage/xxxx.txt".

Can it not access this at that stage?
Probably not. Unless you've added an fstab entry or systemd mount unit for the USB stick yourself it won't get mounted until after the desktop starts.

Plus, as mentioned above, starting GUI programs from rc.local isn't general possible though there are ways to hack around it but they make your desktop very insecure.

For the same reason you can't use an @reboot cronjob either.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Sat Dec 14, 2019 3:20 pm

Thanks, got it working using autostart, with this line:

Code: Select all

@lxterminal  --command sudo python3 /home/pi/max7219/examples/VPsong-10.py

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Sat Dec 14, 2019 9:18 pm

This is not working most of the time, you just see the LXterminal flash up with some text on it and disappear.
Sometimes it loads correctly.

Is there any way to read what is on the LXterminal?

Thanks David

GlowInTheDark
Posts: 371
Joined: Sat Nov 09, 2019 12:14 pm

Re: Run program at Boot

Sat Dec 14, 2019 9:39 pm

Daveolw1 wrote:
Sat Dec 14, 2019 9:18 pm
This is not working most of the time, you just see the LXterminal flash up with some text on it and disappear.
It sounds like something is wrong with the Python program. Are you sure it is fully debugged?
Sometimes it loads correctly.
And sometimes it doesn't. Are you sure it is fully debugged?
Is there any way to read what is on the LXterminal?
Suggestion: Install xterm (sudo apt-get install xterm) and then use the "-hold" option.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

User avatar
thagrol
Posts: 2229
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Run program at Boot

Sat Dec 14, 2019 10:02 pm

GlowInTheDark wrote:
Sat Dec 14, 2019 9:39 pm
Suggestion: Install xterm (sudo apt-get install xterm) and then use the "-hold" option.
Or redirct stdout and stderr to a file when running your program, For example:

Code: Select all

@lxterminal  --command sudo python3 /home/pi/max7219/examples/VPsong-10.py >/home/pi/max7219/examples/VPsong-10.log 2>&1
You won't see any text output in the terminal but should get text output and errors in the log file.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

GlowInTheDark
Posts: 371
Joined: Sat Nov 09, 2019 12:14 pm

Re: Run program at Boot

Sat Dec 14, 2019 11:19 pm

Or, for that matter, make the command part be something like:

Code: Select all

bash -c 'myprogram.py;read -p "Press enter to continue..."'
Note that that is a double quote followed by a single quote at the end of that.

Then you don't need to install xterm.
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

golinux
Posts: 1
Joined: Mon Apr 24, 2017 9:44 pm

Re: Run program at Boot

Sun Dec 15, 2019 10:44 am

I have a similar problem:
I have to start a program (php-cli) without GUI at startup after the raspbian desktop has started, I searched the internet for guides and then modified the file /etc/xdg/lxsession/LXDE-pi/autostart and the program starts, but the only problem is that no console (lxterminal) opens. I'd like to see the program's screen output in the shell/console.
How can I do?

pcmanbob
Posts: 7965
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Run program at Boot

Sun Dec 15, 2019 1:05 pm

If you want the terminal to open and then run your program then you need to do it like this

Code: Select all

 @lxterminal -e python3 /home/pi/your_script.py
 
replacing everything after the -e with your program call.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Sun Dec 15, 2019 4:12 pm

Tried putting in a log file, the first time I booted it my program loaded ok but no log file created. Booting again the lxterminal flashed up with some words on it and disappeared but still no log file created.

Code: Select all

@lxterminal --command sudo python3 /home/pi/max7219/examples/VPsong-19.py >/home/pi/Desktop/log.txt 2>&1
Tried using the bash command, the lxterminal comes up with no title and nothing else.

Code: Select all

@lxterminal --command bash -c  '/home/pi/max7219/examples/VPsong-19.py;read -p "Press enter to continue..."'
Do not no how to solve it.

Thanks David.

GlowInTheDark
Posts: 371
Joined: Sat Nov 09, 2019 12:14 pm

Re: Run program at Boot

Sun Dec 15, 2019 4:29 pm

Did you try using xterm with -hold?

I know that's probably more trouble than you want to go to, but it will be worth it.

I think lxterminal is basically a stripped down xterm and it shows. The whole point of the 'l' programs is to be "light weight", which often means "missing functionality".
GitD's list of things that are not ready for prime time:
1) IPv6
2) 64 bit OSes
3) USB 3
4) Bluetooth

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Sun Dec 15, 2019 7:44 pm

Yes I have installed xterm, but where and what code do I use?
I typed "sudo python3 /home/pi/max7219/examples/VPsong-19.py" in xterm and that runs my program ok, but where do I put it to auto start, tried it in the lxsession autostart with @xterm -hold -e python3 /home/pi/max7219/examples/VPsong-19.py, but that does nothing.

xterm has no menu at the top, file edit etc like Lx so you can't copy/paste, how is this better than Lx.

Under Preferences there is "Desktop Session Settings"
and a option "Automatically Started Applications"
Then "Enable Application"
then two tick boxes 1. not ticked Gsetting Data conversion
2. ticked pprompt

Should there be anything else here?

Have tried following instruction to create a .service to run at boot but that did not work.

Such a simple thing you can do in Windows but in Raspberry so difficult to get a program to run at start.

David

Daveolw1
Posts: 58
Joined: Thu May 16, 2019 10:24 am

Re: Run program at Boot

Sun Dec 15, 2019 8:09 pm

From the VPsong.service I set up I have managed to get a log report as follows:
Traceback (most recent call last):
File "/home/pi/max7219/examples/VPsong-19.py", line 37, in <module>
root = Tk()
File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
I don't why this error occurs because it runs ok when I type in the xterm direct.
Any ideas?

Thanks David

Return to “General discussion”