I am total python/linux beginner and struggeling starting a vncviewer on raspberry pi with buster via pythonscript...
I made a configscript so 1 image can fit in about 20 places. That part works great. Now we got problems with not reconnecting vnc viewers, after complete restart everything works fine... so i thought it would be a good practise to create a pythonscript to monitor the vncviewer and restart just the viewer if needed.
# Importieren der benötigten Module
import time
import subprocess
import socket
#aus IPAdresse zu PCnamen finden
def get_ip_address(hostname):
try:
ip = socket.gethostbyname(hostname)
#print(ip)
return (ip)
except socket.error as e:
print (str(e))
#VNC Server aus der Datei lesen die vom Configskript mit entsprechenden Daten gefüllt wird
def read_vnc_server():
with open('/home/pi/vncremote.vnc', 'r', encoding='ISO-8859-1') as file:
lines = file.readlines()
second_line = lines[1].strip()
#print(second_line)
hostname = second_line[5:]
#print(second_line)
return (hostname)
# netstat Verbindungen nach erwartetem Servernamen filtern, wenn gefunden dann verbunden und return true
def test_connection(vnc_server):
ps = subprocess.run(['netstat', '-an'], check=True, capture_output=True)
vnccon = subprocess.run(['grep', '-e', vnc_server], input=ps.stdout, capture_output=True)
#print(vnccon.returncode)
return vnccon.returncode == 0
while True:
hostname = read_vnc_server()
#print(hostname)
ip = get_ip_address(hostname)
#print(ip)
ip = ip + ':5900'
#print(ip)
#print(test_connection(ip))
if not test_connection(ip):
subprocess.Popen(["/usr/bin/vncviewer", "-config", "/home/pi/vncremote.vnc"])
# Warten für das definierte Zeitintervall
time.sleep(10)
If I run that script using geany in raspberry pi os everything works fine, but starting it as a service will always end up with "unable to open display"""
so I think this line is the problem:
subprocess.Popen(["/usr/bin/vncviewer", "-config", "/home/pi/vncremote.vnc"])
I tried some display:=0 arguments in that line without knowing why...
xhost +localhost
nothing worked
It should be a problem with starting out of shell/service, when starting in raspberry pi os everything works fine. But cant figure out the problem.
Can you help?