Start-up Script In The Foreground
Hello. I recently posted another question on the forum but I recently realised that I'v been asking the wrong question. What i'm trying to do is run a Python script once the Raspberry Pi boots up. I have the Pi launching straight to the GUI and have used stuff like 'Rc.local' to run a script on boot but my script uses inputs and outputs so I really want the script to run as it would in the terminal. Does anyone know of a way to ever run the script that is run using 'rc.local' in the foreground or any other simple techniques that would run my python script in the foreground on boot?
Thanks, Tekk3y
Thanks, Tekk3y
Re: Start-up Script In The Foreground
This works for me. Following the advice given in your previous post, I made sure my python script had the proper shebang and executable mode so that it could be run from a command line. Then I added this hello.desktop file to ~/.config/autostart:
Code: Select all
[Desktop Entry]
Exec=lxterminal -e "/home/pi/hello.py"
Type=Application
Re: Start-up Script In The Foreground
Yehhhhh, got it, thnx 

Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org
Re: Start-up Script In The Foreground
BUT! ..... when I press cntrl+c to stop it the terminal closes as well
Can I get it to just fall back to the prompt?
I want to display some data but leave it on the screen (in the terminal window) so that it can be viewed after halting the python programme.

Can I get it to just fall back to the prompt?
I want to display some data but leave it on the screen (in the terminal window) so that it can be viewed after halting the python programme.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org
Re: Start-up Script In The Foreground
I dunno. I guess I would simply not exit the program until you're done viewing the results.
Re: Start-up Script In The Foreground
They are scrolling of the screen to fast. 

Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org
Re: Start-up Script In The Foreground
I was thinking of something like this (or something more elegant):
Code: Select all
try:
(generate a lot of fast-scrolling data)
except KeyboardInterrupt:
raw_input("Press Enter to Exit")
-
- Posts: 217
- Joined: Sun Mar 16, 2014 10:56 am
Re: Start-up Script In The Foreground
It's a bit messy but could you run a second terminal to monitor the output?
If so you could change the first .desktop file to
Where the temp file could be saved in /dev/shm to avoid SD card wear (not sure how much output you have - I may just be being paranoid
) and then you'd need a second script and a second .desktop to run it. You need to sleep long enough that the first script has made the file, but any output before this had happened would be lost (but still in the file if you need it)
This should leave you with a shell still monitoring the file for more output once the first script has finished.
If so you could change the first .desktop file to
Code: Select all
Exec=lxterminal -e nohup "/home/pi/hello.py" >/somewhere/for/a/temp/file

Code: Select all
#!/bin/sh
sleep 2
tail -f /somewhere/for/a/temp/file
This should leave you with a shell still monitoring the file for more output once the first script has finished.
Re: Start-up Script In The Foreground
So i'v got it partially working. LXterminal comes up on boot but disappears after about as second. As far as I can tell none of the program is being run as there are inputs that should be waiting for a user? Do any of you know why this may be?
Thanks, Tekk3y
Thanks, Tekk3y
Re: Start-up Script In The Foreground
So I'v played around with it a bit and now LXTerminal comes up but doesnt run the program or even allow for inputs like Ctrl + C, etc.Tekk3y wrote:So i'v got it partially working. LXterminal comes up on boot but disappears after about as second. As far as I can tell none of the program is being run as there are inputs that should be waiting for a user? Do any of you know why this may be?
Thanks, Tekk3y
Anyone have any ideas on what's wrong?
Tekk3y
Re: Start-up Script In The Foreground
Probably not, without knowing what 'playing around' entails. Try posting your desktop file and a short python script that demonstrates the issue you're having.
Re: Start-up Script In The Foreground
Hi. I'v attached an image of both the desktop file and the start of my Python file.


I thought that I should also mention that I made the file executable by typing chmod +x program.py into the terminal just in case that's wrong.
This is also a screen shot of what comes up on boot:

Hope this help. Thanks, Tekk3y


I thought that I should also mention that I made the file executable by typing chmod +x program.py into the terminal just in case that's wrong.
This is also a screen shot of what comes up on boot:

Hope this help. Thanks, Tekk3y
Re: Start-up Script In The Foreground
If memory serves, using RPI.GPIO requires that the script be run as root. You can try adding 'sudo' like this, but no guarantees
Is program.py still working when you type it into an LXTerminal? Does it run properly without the sudo in that case?
When debugging, it's always a good idea to fall back to a simple test case. Start with a script that prints out "Hello world" and sleeps for three seconds. Test it. Then add some user input. Then add the GPIO, etc. See when it "breaks", that will be a good indication of why. And make sure it works as desired when manually started, before trying to autostart it.
Code: Select all
Exec=lxterminal -e "sudo /home/pi/Desktop/program.py"
When debugging, it's always a good idea to fall back to a simple test case. Start with a script that prints out "Hello world" and sleeps for three seconds. Test it. Then add some user input. Then add the GPIO, etc. See when it "breaks", that will be a good indication of why. And make sure it works as desired when manually started, before trying to autostart it.
Re: Start-up Script In The Foreground
Hi. Program.py does require root and when I type into the terminal normally it works fine. When I add 'sudo' to the front of the code the terminal no longer stays there and just disappears straight away.
I have also tried it with a simple hello world program in a loop but it does the exact same thing which is why I though it may have something to do with how I made the program executable or the hash and the start of the program.
Thanks, Tekk3y
Code: Select all
sudo python program.py
I have also tried it with a simple hello world program in a loop but it does the exact same thing which is why I though it may have something to do with how I made the program executable or the hash and the start of the program.
Thanks, Tekk3y
Re: Start-up Script In The Foreground
I get exactly the same so it's not you.Tekk3y wrote:Hi. Program.py does require root and when I typeinto the terminal normally it works fine. When I add 'sudo' to the front of the code the terminal no longer stays there and just disappears straight away.Code: Select all
sudo python program.py
I have also tried it with a simple hello world program in a loop but it does the exact same thing which is why I though it may have something to do with how I made the program executable or the hash and the start of the program.
Thanks, Tekk3y
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org
Re: Start-up Script In The Foreground
Didn't you say that yours only stopped when you pressed Ctrl + C? Mine doesn't even run the program to begin with.Cancelor wrote: I get exactly the same so it's not you.
-Tekk3y
Re: Start-up Script In The Foreground
Nah, that was yesterday but I've re-booted since then and now it's same as yours!
So what can it be? I dunno ... we both have a fairly new set up, we have both had to manually created the autostart directory
There is a missing step somewhere. We need help doing some proper diagnosis.
When you have the frozen terminal does your mouse work? Sometimes mine doesn't

So what can it be? I dunno ... we both have a fairly new set up, we have both had to manually created the autostart directory
There is a missing step somewhere. We need help doing some proper diagnosis.
When you have the frozen terminal does your mouse work? Sometimes mine doesn't

Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org
Re: Start-up Script In The Foreground
I'v had a look and its nothing to do with the actual .py file as even when a bogus file name is entered it does the exact same thing. I was thinking it might be something to do with the file path as maybe something is missing. I definitely feel that the problem is in the .desktop file somewhere after the lxterminal section but other than that i'm fairly new to this so I have no idea. :/Cancelor wrote:Nah, that was yesterday but I've re-booted since then and now it's same as yours!![]()
So what can it be? I dunno ... we both have a fairly new set up, we have both had to manually created the autostart directory
There is a missing step somewhere. We need help doing some proper diagnosis.
When you have the frozen terminal does your mouse work? Sometimes mine doesn't
Re: Start-up Script In The Foreground
If yours was working yesterday you need to see what you have done differently since then.Cancelor wrote:Nah, that was yesterday but I've re-booted since then and now it's same as yours!:
Re: Start-up Script In The Foreground
I'm not at my Pi, and I've about run out of ideas, but I would also try re-jiggering the .desktop file so the Exec looks like this:
in the case where the script needs sudo. Also, remove any rc.local, .bashrc etc. entries from previous attempts (if any), that might be interfering. As for the .desktop file, I did some reading, and supposedly Name= is required, so that won't hurt. I'm unclear what Terminal=true is spossed to do, but it's worth a try also.
Code: Select all
Exec=sudo lxterminal -e "/home/pi/Desktop/program.py"
Re: Start-up Script In The Foreground
I dont use lxterminal but have found what appears to be
a working method for foreground scripts .. cant hurt to try with lx

Code: Select all
sudo lxterminal -e '/bin/sh' -c '/home/pi/Desktop/program.py ; exec /bin/sh'
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?
Re: Start-up Script In The Foreground
Hi, Thanks for both trying to help although none of those changes did anything and it still just makes the terminal pop up for a second. :/
Re: Start-up Script In The Foreground
It took me a while to get working , so persistence may well pay off , ...Tekk3y wrote:Hi, Thanks for both trying to help although none of those changes did anything and it still just makes the terminal pop up for a second. :/
You could try getting it working from with in a terminal first then once it is working adding the working command string to the start up script is easy enough , at least that is the method i used to get to my solution!
What happens when you try typing this
Code: Select all
lxterminal -e '/bin/sh' -c ' sudo /home/pi/Desktop/program.py ; exec /bin/sh'
in a terminal ?
-- edit perhaps sudo should moved idk!!!
Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?
Re: Start-up Script In The Foreground
Hi. Running that command in the terminal does the same thing. This being that it will open the terminal for a split second then it closes again. I dont know if its worth mentioning but when I click on my 'program.py' file on my desktop and then click the prompt for 'Execute in terminal' nothing comes up. Could this be the problem or is that usual?
Thanks.
Thanks.
Re: Start-up Script In The Foreground
This command sting works (for me)
How\why it works http://explainshell.com/explain?cmd=lxt ... n%2Fsh+%27
Replace the "echo this sh works " with the path to your script and prefix with sudo if needed , enjoy
.
Code: Select all
lxterminal -l -e 'echo this sh works ; /bin/sh '
Replace the "echo this sh works " with the path to your script and prefix with sudo if needed , enjoy

Real life is, to most, a long second-best, a perpetual compromise between the ideal and the possible.
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?
-
Meanwhile, the sysadmin who accidentally nuked the data reckons "its best not run anything more with sudo today"
-
what about spike milligan?