0

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.

1
  • You can't run GUI apps as root. Perhaps you need to do auto-login and have this as part of your .bashrc. Commented Mar 16 at 17:38

1 Answer 1

1

I already ran into a similar issue, and for me it was caused by the fact that cron does not have access to your variables so if it tries to display something, the $DISPLAY variable is not set so it doesn't know where to send the graphical output.

Find the value of $DISPLAY in your shell:

echo $DISPLAY

For me it says that $DISPLAY is :0, so I have to export this variable before running the cron task:

@reboot sleep 60 && export DISPLAY=:0 && /usr/bin/python3 /home/pi/ads.py

If this doesn't work, as Tim Roberts ponted out it can be that on some systems you cannot use graphical interfaces as root, so if that's possible for your usecase you might want to add the task to your user crontab instead of the root one

sudo crontab -u username -e
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, but I had to use it this way: @reboot sleep 60 && export DISPLAY=:0 && /usr/bin/python3 /home/pi/ads.py I guess the system needed more time before I could export the variable. Thanks so much!!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.