Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Start-up Script In The Foreground

Sat Jun 21, 2014 6:14 pm

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

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Sat Jun 21, 2014 8:28 pm

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

User avatar
Cancelor
Posts: 754
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start-up Script In The Foreground

Sat Jun 21, 2014 9:40 pm

Yehhhhh, got it, thnx :mrgreen:
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Cancelor
Posts: 754
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start-up Script In The Foreground

Sun Jun 22, 2014 6:25 am

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't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Sun Jun 22, 2014 6:42 am

I dunno. I guess I would simply not exit the program until you're done viewing the results.

User avatar
Cancelor
Posts: 754
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start-up Script In The Foreground

Sun Jun 22, 2014 6:56 am

They are scrolling of the screen to fast. :(
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Sun Jun 22, 2014 7:11 am

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")

suicidal_orange
Posts: 217
Joined: Sun Mar 16, 2014 10:56 am

Re: Start-up Script In The Foreground

Sun Jun 22, 2014 7:16 am

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

Code: Select all

Exec=lxterminal -e nohup "/home/pi/hello.py" >/somewhere/for/a/temp/file
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

Code: Select all

#!/bin/sh
sleep 2
tail -f /somewhere/for/a/temp/file
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.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 1:50 pm

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

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 2:28 pm

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
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.
Anyone have any ideas on what's wrong?

Tekk3y

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 2:42 pm

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.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 2:54 pm

Hi. I'v attached an image of both the desktop file and the start of my Python file.
Image
Image

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:
Image

Hope this help. Thanks, Tekk3y

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 3:26 pm

If memory serves, using RPI.GPIO requires that the script be run as root. You can try adding 'sudo' like this, but no guarantees

Code: Select all

Exec=lxterminal -e "sudo /home/pi/Desktop/program.py"
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.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 3:41 pm

Hi. Program.py does require root and when I type

Code: Select all

sudo python program.py
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

User avatar
Cancelor
Posts: 754
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 4:57 pm

Tekk3y wrote:Hi. Program.py does require root and when I type

Code: Select all

sudo python program.py
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
I get exactly the same so it's not you.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 5:01 pm

Cancelor wrote: I get exactly the same so it's not you.
Didn't you say that yours only stopped when you pressed Ctrl + C? Mine doesn't even run the program to begin with.

-Tekk3y

User avatar
Cancelor
Posts: 754
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 8:12 pm

Nah, that was yesterday but I've re-booted since then and now it's same as yours! :cry:

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 :cry:
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 8:32 pm

Cancelor wrote:Nah, that was yesterday but I've re-booted since then and now it's same as yours! :cry:

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 :cry:
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. :/

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 8:34 pm

Cancelor wrote:Nah, that was yesterday but I've re-booted since then and now it's same as yours!:
If yours was working yesterday you need to see what you have done differently since then.

User avatar
Douglas6
Posts: 4473
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 9:11 pm

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:

Code: Select all

Exec=sudo lxterminal -e "/home/pi/Desktop/program.py"
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.

User avatar
r3d4
Posts: 950
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Start-up Script In The Foreground

Mon Jun 23, 2014 9:33 pm

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' 
hmmm - voltaire
.
Real life is, to most , a long second-best, a perpetual compromise between the ideal and the possible.
what about spike milligan??
.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Tue Jun 24, 2014 11:09 am

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. :/

User avatar
r3d4
Posts: 950
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Start-up Script In The Foreground

Tue Jun 24, 2014 12:08 pm

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. :/
It took me a while to get working , so persistence may well pay off , ...

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!!!
hmmm - voltaire
.
Real life is, to most , a long second-best, a perpetual compromise between the ideal and the possible.
what about spike milligan??
.

Tekk3y
Posts: 34
Joined: Sun Feb 23, 2014 1:40 pm

Re: Start-up Script In The Foreground

Tue Jun 24, 2014 12:35 pm

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.

User avatar
r3d4
Posts: 950
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Start-up Script In The Foreground

Tue Jun 24, 2014 1:06 pm

This command sting works (for me)

Code: Select all

lxterminal -l -e 'echo this sh works ; /bin/sh ' 
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 :D .
hmmm - voltaire
.
Real life is, to most , a long second-best, a perpetual compromise between the ideal and the possible.
what about spike milligan??
.

Return to “Beginners”