1

I try to start a python program (for accessing the camera, but that is irrelevant) every time the raspberry pi boots.

The tutorials I read said one should add the corresponding command in /etc/rc.local. In my case the command is (sleep 10;python /home/pi/Desktop/execute_on_boot.py)&

If I enter this exact command into the terminal, it works fine, but I am not sure whether it works after booting, because there is no terminal showing me the outputs of that script. It would be really handy to have an open terminal for that python program.

So is it somehow possible to open a terminal from python? Or can I alter the command somehow in order to automatically allow a terminal?

Or should a terminal open anyway but I did something wrong?

flawr
  • 111
  • 1
  • 1
  • 4

5 Answers5

3

As @joan said, programs started by cron or in rc.local do not have an associated terminal.

However, as long as you are configured to startup using the GUI (set in raspi-config), you can manually launch an instance of lxterminal and execute your command (this assumes raspbian).

So in your case, run this as your rc.local command:

lxterminal -e python /home/pi/Desktop/execute_on_boot.py

You will probably want to put the sleep command in the execute_on_boot.py script, and add one at the end of the script as well since the terminal window will disappear as soon as the script completes.

I tried putting the sleep command after the "-e" and before python, but couldn't figure out how to make it work. If you do figure it out, please post it here.

If you need to delay it until the windowing system starts up, then use cron. To add the cron command type:

crontab -e

Then add this line to the end of the file (untested):

@reboot sleep 30; lxterminal -e python /home/pi/Desktop/execute_on_boot.py

Remember, cron will run as you, so use sudo in the cron command if you need to do something requiring access.

Hope that helps.

ifermon
  • 674
  • 5
  • 11
  • Thank you very much for this suggestion, that looks promising, I'm gonna try that! – flawr Aug 20 '15 at 09:20
  • It does not seem to work, even with the lxterminal command alone. I read somewhere that rc.local gets executed before the user is logged in, so there is no way to get an input or output (whether opening a console or writing a file). So the problemw as that my approach did not work at all. Do you know another method of starting a script e.g. after login? – flawr Aug 20 '15 at 09:56
  • 1
    Added comment on how to add to cron file - that should give you the ability to delay. – ifermon Aug 20 '15 at 11:22
2

Programs started by cron or in rc.local or in a similar fashion do not have an associated keyboard or screen.

What you can do is redirect output to a file and check the file contents periodically, e.g.

(sleep 10;python /home/pi/Desktop/execute_on_boot.py >/tmp/my_program_output)&

joan
  • 71,024
  • 5
  • 73
  • 106
  • That is an elegant alternative, thanks for posting! But for debugging purposes it would still be great to have a terminal for interaction. But I'll see how far I can get with this, so thank you! – flawr Aug 20 '15 at 09:19
  • also you don't have to poll the file: open a terminal and type tail -f /tmp/my_program_output – M Noit Aug 21 '15 at 11:19
2

You can configure the Pi to auto login to the console, see Auto-login with GUI disabled in Raspbian. Then you can put your command in .bashrc in your home directory:

if [ ! -e /tmp/OnlyOneInstance ] ; then 
  python /home/pi/Desktop/execute_on_boot.py &
  touch /tmp/OnlyOneInstance
fi

Note: Your program is now executed as user pi, which is not suitable if you need root privileges.

Brick
  • 1,377
  • 2
  • 13
  • 19
M Noit
  • 926
  • 5
  • 7
1

Here is the solution that I constantly use.

Create a desktop file

xyz.desktop

type the following into it

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=<Application Name Goes here>
Comment=
Exec=  python /home/pi/Desktop/execute_on_boot.py
StartupNotify=false
Terminal=true
Hidden=false

paste this file into the

/home/pi/.config/autostart/

and restart your raspberry pi and it should automatically run your program in a new terminal

evolutionizer
  • 296
  • 2
  • 6
  • This seems to be the cleanest method so far, but I was not able to make it work. Do I have to change some settings somewhere else? I am asking because I had to create the autostart folder in .config. – flawr Aug 21 '15 at 18:26
  • That's the only thing you will have to do. Once you have created the file, run it to be sure it works. If yes then restart the PI and see if it autostartz – evolutionizer Aug 21 '15 at 19:05
  • How exactly can I run the file? – flawr Aug 21 '15 at 19:06
  • ./home/pi/.config/autostart/xyz.desktop

    Typed this on my phone, so hopefully should be all fine.

    – evolutionizer Aug 21 '15 at 19:11
1

You can direct output to a specific tty easily enough:

(
    exec &> /dev/tty4
    sleep 10;
    python /home/pi/Desktop/execute_on_boot.py
) &

You should be able to see the output after you boot up with alt-ctrl-F4. There are 6 such virtual terminals (VTs), the GUI is running inside one of them, probably tty1 (alt-ctrl-F1).

You can scroll up in a plain text console with shift-pageUp, but it only retains what's appeared since you switched to that VT; if you switch to another one and back again, anything that's scrolled up is lost.

You could combine that with joan's logging idea by using tee:

python /home/pi/Desktop/execute_on_boot.py | tee /var/log/myboot.log
goldilocks
  • 58,859
  • 17
  • 112
  • 227