I have a Raspberry Pi 4 that I use at different locations to run a digital display. I use this script to run at reboot so that I don't have to determine the IP address each time and just start playing the ads on the screen. The rpi is connected to the TV via HDMI cable. My crontab -e is as follows:
@reboot sleep 60 && /usr/bin/python3 /home/pi/ads.py
and the script is as follows:
#!/usr/bin/env python3
import socket
import subprocess
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
ip_address = s.getsockname()[0]
s.close()
return ip_address
except Exception:
print("Error getting IP address.")
return None
def launch_fullscreen_chromium():
ip = get_ip_address()
if ip:
command = f"chromium-browser --start-fullscreen --noerrdialogs --hide-crash-restore-bubble --disable-gpu 'http://{ip}/index.php'"
subprocess.call(command, shell=True)
else:
print("Could not retrieve IP address.")
if __name__ == "__main__":
launch_fullscreen_chromium()
When I run the script from the terminal it runs just fine, however from crontab -e I get the following error from my log:
[186875:186875:0318/202225.133327:ERROR:ozone_platform_x11.cc(234)] Missing X server or $DISPLAY
[186875:186875:0318/202225.133388:ERROR:env.cc(225)] The platform failed to initialize. Exiting.
I've tried everything I can think of. cron daemon is running. I simply cannot solve this problem.
.bashrc.