5

I am trying to get my Raspberry Pi to open a terminal and run a Python script when it boots up. I have followed the instructions of some guides and have so far been unsuccessful.

My steps

I have made a Python file called testfile.py located in /home/pi/Documents/testfile.py

The contents are as follows:

print('hello')
input('press any key to exit')

I have added a file called testfile.desktop to /home/pi/.config/autostart/testfile.desktop.

The contents are as follows:

[Desktop Entry]
Type=Application
Name=testfile
Hidden=false
NoDisplay=false
Terminal=true
X-GNOME-Autostart-enabled=true
Exec=/usr/bin/python3 /home/pi/Documents/testfile.py

When I double click this file, the terminal opens and the Python script runs as expected, but when I reboot the Raspberry Pi the script doesn't run.

4

I duplicated what you did using an up-to-date Raspbian Buster in a Pi 4 B. (I wish all questions were as clear and complete as yours!)

Double-clicking the file on my machine brought up the thonny IDE. (Note: The 'any' key doesn't work because input() needs an enter to complete.) On reboot, I observed the same result you did. However, installing xterm and changing the Exec line to

Exec=xterm -hold -e '/usr/bin/python3 /home/pi/Documents/testfile.py'

causes an xterm window to open and the Python program to run. I was following these debugging instructions.

So, there is some interaction I don't understand between autostart, Python, and terminal. Perhaps you could try a Python program that leaves some evidence in the file system, such as writing a tiny output file.

Edit: See this answer which appears to be more useful than mine.

| improve this answer | |
  • 1
    That seems to have done it. I was following the same guide you linked, but didn't catch the xterm installation part (should have added that to the original question). – Achmed Mar 22 at 16:17
  • 1
    If you discover why it doesn't work without xterm, please let everyone know. – Bob Brown Mar 22 at 16:25
  • 2
    Found an alterative way using x-terminal-emulator instead of xterm. (raspberrypi.stackexchange.com/questions/27581/…). still not sure why normal exec doesn't work and i can't find a way of getting to the logs of the startup. – en_lorithai Mar 22 at 16:45
2

Create a file:

nano /etc/systemd/system/startupbrowser.service  

Put all lines below there:

[Unit]
Description=testfile service

[Service]
ExecStart=/usr/bin/python /home/pi/Documents/testfile.py
StandardOutput=syslog
StandardError=syslog
Restart=on-failure

User=root
Group=root
SyslogIdentifier=testfile

[Install]
WantedBy=multi-user.target  

Save the file and reload the daemon:

sudo systemctl daemon-reload

Test that is your code is running:

sudo service testfile start

Check the log by:

journalctl -f -u testfile.service

It will give you the real-time log of your python code to troubleshoot what's going on.

If everything was good as you intended, run this command to enable is as a startup service:

sudo systemctl enable testfile.service
| improve this answer | |
  • 2
    This will run the python script as a sevice, which is interesting, but will it show the terminal and the outputs of the script? – Achmed Mar 22 at 16:18
  • 1
    @Achmed Yes, easily. The answer has updated, check it again. – M. Rostami Mar 22 at 22:01
  • @PeterMortensen Thank you. I have changed the second part but I think that was not distinguished. – M. Rostami Mar 23 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.