You are not logged in.

#1 2025-07-11 11:18:28

sebastian-65
Member
Registered: 2021-11-08
Posts: 40

[SOLVED] Cron: can't open display

I'm trying hard to prevent getting this error from each user cronjob (cronie) that runs my python scripts.

Methods I've tried:
-  putting DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus and DISPLAY=:0 in cron head
-  * * * * * env DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus  DISPLAY=:0 "path/script.py"
-  * * * * * /usr/bin/xuserrun "path/script.py"
-  * * * * *  "path/script.py" > /dev/null 2>&1 # this denies me a possibility to get other meaningful errors I need to catch
- importing environment from within python scripts

    gui_env_dynamic = {
        'DISPLAY': current_display,  # Dynamic display detection
        'XAUTHORITY': xauthority_file,  # Dynamic XAUTHORITY detection
        'DBUS_SESSION_BUS_ADDRESS': f'unix:path=/run/user/{user_id}/bus',
        'HOME': home,
        'USER': person,
        'LOGNAME': person,
        'PATH': '/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl',
        'XDG_RUNTIME_DIR': xdg_runtime,
        'XDG_SESSION_ID': '1',
        'XDG_SESSION_TYPE': 'x11',
        'XDG_SESSION_CLASS': 'user',
        'XDG_SEAT': 'seat0',
        'XDG_CURRENT_DESKTOP': 'XFCE',
        'XDG_MENU_PREFIX': 'xfce-',
        'XDG_CONFIG_DIRS': '/etc/xdg/xdg-xfce:/etc/xdg',
        'XDG_DATA_DIRS': '/usr/share/xfce4:/usr/local/share:/usr/share',
        'XDG_CONFIG_HOME': xdg_config,
        'XDG_DATA_HOME': xdg_data,
        'XDG_CACHE_HOME': xdg_cache,
        'XDG_VTNR': '2',
        'XDG_SEAT_PATH': '/org/freedesktop/DisplayManager/Seat0',
        'XDG_SESSION_PATH': '/org/freedesktop/DisplayManager/Session0',
        'XDG_SESSION_DESKTOP': 'XFCE',
        'PRIMARY_DISPLAY': 'eDP1',
        'ORIGINAL_XDG_CURRENT_DESKTOP': 'XFCE',
        # GNOME Keyring environment variables
        'GNOME_KEYRING_CONTROL': f'{xdg_runtime}/keyring',
        'GNOME_KEYRING_PID': '',
        'SSH_AUTH_SOCK': f'{xdg_runtime}/keyring/ssh'
    }

.xinitrc contents:

source /etc/X11/xinit/xinitrc.d/50-systemd-user.sh
exec startxfce4

- I've read docs about cron, running GUI apps, env and whatnot, yet still haven't figured it out properly.
- Using both laptop display and external monitor (tried DISPLAY=:0.0 also)

Any idea how to set display right?

Last edited by sebastian-65 (2025-07-11 16:50:41)


I'm not the sharpest tool in the shed. Say it to me like I'm 5, please!

Arch Linux | ZFS | systemd | Xfce/X11/SDDM | ASUS S435 | Intel CPU 11th gen | Intel TigerLake-LP GT2 | 16GB RAM

Offline

#2 2025-07-11 12:20:29

karabaja4
Member
From: Croatia
Registered: 2008-09-14
Posts: 1,048
Website

Re: [SOLVED] Cron: can't open display

Try something like this:

cron

* * * * *    path/run.sh

run.sh

#!/bin/sh

export XAUTHORITY="${HOME}/.Xauthority"
export DISPLAY=":0"

exec path/script.py

The "export" is the key as it passes the environment variables to the child process.

Last edited by karabaja4 (2025-07-11 12:21:14)

Offline

#3 2025-07-11 16:34:01

sebastian-65
Member
Registered: 2021-11-08
Posts: 40

Re: [SOLVED] Cron: can't open display

I've found out I had perhaps a garbled .Xauthority file, so I replaced the contents temporary file (from xauth info) and it worked automagically again!

You've definitely shown me the way, thank you stranger!


I'm not the sharpest tool in the shed. Say it to me like I'm 5, please!

Arch Linux | ZFS | systemd | Xfce/X11/SDDM | ASUS S435 | Intel CPU 11th gen | Intel TigerLake-LP GT2 | 16GB RAM

Offline

#4 2025-07-11 16:42:22

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,442

Re: [SOLVED] Cron: can't open display

Also https://gist.github.com/AladW/de1c5676d93d05a5a0e1

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Offline

Board footer

Powered by FluxBB