Bad Display Name when running Python script on boot with Touch Screen

别说谁变了你拦得住时间么 提交于 2020-01-30 10:12:04

问题


Attempting to run a Python script on boot on Raspberry Pi 3B+ 1GB RAM, Raspbian, with a SunFounder 10" Touch Screen, - .log file returns "Bad display name'

Python script is 100% functional when run via Terminal / executable script / Thonny etc. Attempted to run at boot first via rc.local - created a service, enabled service, daemon-reload... etc. Did not work. Tried to run as crontab, same result - .log output from crontab shows "Bad display name". Thought it was lack of Display Environment imported and declared within the Python script, so I added that - but on boot returns the same result.

This is the Python Script I'm using

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import os
import sys
import webbrowser
import time
import subprocess
from pynput import keyboard
from Xlib.display import Display

#GPIO Readout
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#GPIO Header Setup
header = 2
GPIO.setup(header, GPIO.IN)

#Omxplayer Commands
Loop = 'omxplayer -b --loop --no-osd -o hdmi /home/pi/Videos/PlanetEarth.mp4 > /dev/null'
Donation = 'omxplayer -b --no-osd -o hdmi /home/pi/Videos/Cartoon.mp4 > /dev/null'
KillPlayer = 'pkill omxplayer.bin'
KillForm = 'pkill chromium'

#Set Display Environment
new_env = dict(os.environ)
new_env['DISPLAY'] = ':0.0'

#Form Handling Required Below
#If Donation is successful, Stop Looping Video, Open Form in Chromium, Wait 60 seconds, Close Chromium, Restart Loop
def PullDownSuccess():
    subprocess.Popen(KillPlayer, env=new_env, shell=True)
    time.sleep(2)
    webbrowser.open('<url>')
    time.sleep(60)
    subprocess.Popen(KillForm, env=new_env, shell=True)
    time.sleep(2)
    subprocess.Popen(Loop, env=new_env, shell=True)

#Inception
subprocess.Popen(Loop, env=new_env, shell=True)

#Terminate Loop with Escape Key or Manually Initiate Donation Success
def on_press(key):
    if key == keyboard.Key.ctrl:
        PullDownSuccess()
    if key == keyboard.Key.esc:
        subprocess.Popen(KillPlayer, shell=True)

#Keyboard Listener Module
with keyboard.Listener(
        on_press=on_press) as listener:
    listener.join()

#Donation Successful Do:
while True:
    header_state = GPIO.input(header)
    if header_state == GPIO.HIGH:
        PullDownSuccess()

I am currently attempting to run this script on Boot via crontab with this line:

@reboot (/bin/sleep 10; /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py > /home/pi/Custom_Scripts/cronjoblog 2>&1)

The error log file for crontab returns the following:

    raise error.DisplayNameError(display)
Xlib.error.DisplayNameError: Bad display name ""

This error only exists when attempting to run script on boot up. Is the Display overriding the boot display permissions on Boot Up? What is keeping the script from running on the Display on boot up, but not when remotely executed? Thank you for your consideration.


Update: Still no solution. Display environment returns ":0.0' ... so far I have tried to remove

> /dev/null from #Omxplayer Commands

Replacing crontab startup line to:

DISPLAY=":0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

and

DISPLAY=":0.0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

And any possible combination of these.

Confirmed the script is not waiting for any background processes as I have added delay (time.sleep) up to 30 seconds, as well as returning IP addresses, etc.

Returns either Bad Display Name still OR "Can't connect to display ":0": b'Invalid MIT-MAGIC-COOKIE-1 key"

Still looking for a solution if anyone has one.

EDIT: Fixed using /LXDE-pi/autostart. Answer below.


回答1:


Try to add DISPLAY environment variable before calling your script:

DISPLAY=":0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py



回答2:


DISPLAY="/dev/null" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

Have you tried a longer sleep?




回答3:


If you try to get your ip adress during your startup script with (for example) :

  import socket
  def get_ip_address():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        return s.getsockname()[0]

you will get an errror like:

File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 101] Network is unreachable

This is because at reboot network is not necessarily available. In order to make it available see https://raspberrypi.stackexchange.com/questions/45769/how-to-wait-for-networking-on-login-after-reboot :

sudo raspi-config

then select option 3 Boot Options then select option 4 Wait for network at boot

Once you have done that networking should not be an issue at reboot.

You get also check for other ways to enforce network setup before running your script take a look at : https://askubuntu.com/questions/3299/how-to-run-cron-job-when-network-is-up




回答4:


If there are unforeseen ramifications for using this method, I will update this thread accordingly. I fixed this issue just by adding two lines to the autostart file which resides in /etc/sdg/lxsession/LXDE-pi/autostart.

sudo nano /etc/xgd/lxsession/LXDE-pi/autostart

Add these two lines, do not modify the existing code.

sleep 5
@/usr/bin/python3 /home/pi/Custom_Scripts/<script>.py

My autostart file looks like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
point-rpi
sleep 5
@usr/bin/python3 /home/pi/Custom_Scripts/<script>/py

Ctrl + O to write file

sudo reboot


来源:https://stackoverflow.com/questions/57997480/bad-display-name-when-running-python-script-on-boot-with-touch-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!