I have a Python script that runs at startup and looks for GPIO inputs (the buttons on my PiTFT screen). I can make the buttons do things like shut down the Pi (sudo shutdown now), but I'm stumped as to how to make the buttons launch applications like lxterminal or pithos. Here's what I have for the script - can anyone suggest why the button for shutdown works but the buttons for launching apps doesn't? I have added a line in my /etc/rc.local that starts the script at startup via sudo, if that helps....

# a simple script for using the tactile buttons on the TFT

import RPi.GPIO as GPIO
import time
import os

# Use Broadcom SOC Pin Numbers
# setup with internal pullups and pin in READ mode

GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

#  Main Loop

while 1:
    if ( GPIO.input(22) == False ):
        os.system("sudo pithos")
    elif ( GPIO.input(27) == False ):
        os.system("sudo lxterminal --geometry=40x10")
    elif ( GPIO.input(18) == False ):
        os.system("sudo shutdown -h now")
  • Have you confirmed the code takes the paths by adding a print statement? By the way you should only use sudo when needed.
    – joan
    Commented Apr 15, 2016 at 8:12
  • does it help if you add sleep before the python launch command. All my scripts in python use it. I think it should help
    – Shan-Desai
    Commented Nov 19, 2016 at 20:09

3 Answers 3


You may use subprocess.call() function instad of os.system (reference link)

import subprocess
subprocess.call(["sudo", "lxterminal", "--geometry=40x10"])
  • I tried that and the apps still don't launch. The Pi boots to the desktop, I punch the buttons, and still the only command that works is the one that does the shutdown.... :(
    – Wolfgang
    Commented Apr 15, 2016 at 1:58

This might be caused by some missing environment variables; namely the DISPLAY variable. It would explain why a command that doesn't use the display, e.g. sudo shutdown -h now, works while sudo lxterminal --geometry=40x10 doesn't.

Assuming that your display is numbered 0 (default), try adding this at the top of your python script:

import os
os.putenv('DISPLAY', ':0')

It essentially tells the system where to send your lxterminal window in case you're not starting the process manually/from terminal. You can see the number of your display by typing echo $DISPLAY in terminal:

$ echo $DISPLAY
  • Tried that - the PiTFT LCD display had a display value of :0.0 - and still nothing. I think I need to figure out how to make my script write a logfile so that I can try to debug it that way....
    – Wolfgang
    Commented Apr 16, 2016 at 18:16
  • @Wolfgang Yeah, that's always a good idea; so is using the subprocess module instead of os.system and writing out the full path to executables such as lxterminal; e.g. import subprocess, subprocess.call(["/usr/bin/lxterminal", "--geometry=40x10"])
    – jDo
    Commented Apr 16, 2016 at 19:16

First of all, if you're running the script from rc.local you don't need to execute commands with sudo because they already are running with root privileges. It's redundant.

Have you confirmed that the commands you are attempting to execute are actually installed? Try running them manually from the command line.

A simple way to allow debugging is to simply insert print statements in your code:

print "debug: attempting to launch lxterminal"

They should show up in a system log, or you could redirect them to someplace specific by modifying the line that calls your script in rc.local like this:

/usr/local/bin/script.location.py >> /var/log/custom.script.log 2>&1

Maybe that will provide more information you can use to figure out what is going on.

Best of luck!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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