Driving a desktop from a headless agent shell (DISPLAY, clipboard, sudo hand-off)
x11xclipclipboardsudopkexecclaude-codeshellgtk4screenshots
An agent's shell (Claude Code, a cron job, a systemd user service) runs without the user's graphical environment variables and without a terminal for passwords. On an X11 desktop most of that can be bridged: GUI apps and the clipboard are reachable by setting DISPLAY, and root commands can be handed to the user with one paste. On Wayland-only sessions it cannot, because the shell has no Wayland socket.
Launching GUI apps
WAYLAND_DISPLAYis unset and/run/user/1000/has no Wayland socket in such shells, so Wayland-native launches fail with Qt platform plugin errors.- On X11:
DISPLAY=:N app &(background long-running apps, e.g.DISPLAY=:N setsid nohup steam steam://rungameid/2225070).xdg-open URLworks the same way. - The display number is not fixed: it increments after each X server crash. Read it with
ps -C Xorg -o args=rather than assuming:1.
Screenshots of an app you just built
- Do not use
spectacle -a(or any "active window" grab) from an agent shell: it captures whatever window has focus, which was the user's browser, not the new app. - For a GTK4 app, render the window offscreen instead and save the PNG: build the window in a
script (use a
NON_UNIQUEapplication id so it does not hand off to a running instance), wrap it inGtk.WidgetPaintable, snapshot that, render it to a texture with the widget's renderer (render_texture), andtexture.save_to_png(). No focus change, nothing visible on the user's screen. Used for the~/dev/calcdesktop calculator (2026-10-23). - For web pages use headless Chrome (Headless Chrome testing gotchas).
Installing a local app into the desktop menu
A per-user install needs no root: icons into ~/.local/share/icons/hicolor/<size>/apps/, a
.desktop file into ~/.local/share/applications/<app-id>.desktop, and a symlink into
~/.local/bin for a command. Keep it in an install.sh that also regenerates the icon, so it can
be rerun after changes.
Clipboard hand-off (instead of "please copy this")
- Single command:
printf '%s' 'THE COMMAND' | DISPLAY=:N xclip -selection clipboard, then tell the user it is on the clipboard. (wl-copyfails here; xclip over X works. A clipboard manager such as copyous keeps it after xclip exits.) - Several commands: write a script to
/tmp, putbash /tmp/script.sh > /tmp/out.txt 2>&1on the clipboard, then read the output file when the user says it ran. - Interactive prompts: use
2>&1 | tee /tmp/out.txtinstead of>so the user sees the prompt; echo-off password input never lands in the file. - For sudo scripts, make
sudo -vthe first line so the password prompt appears on the terminal, then run the redirected commands.
Root without typing a password
pkexec cmdshows a graphical polkit prompt if a polkit agent runs in the session (KDE:polkit-kde-authentication-agent-1).- logind lets the active seat user reboot:
systemctl reboot(check withbusctl ... CanReboot). - On a remote server where the sudo password is known to the session,
ssh host "echo '<pw>' | sudo -S -p '' cmd"works; never pipe a script tosudo -Son stdin, because sudo consumes it (more stdin traps in Heredoc, stdin and scripted-edit pitfalls). The password lives in the user's global CLAUDE.md, not here.
Gotchas
pgrep -f Xandpkill -f Xmatch the agent's own wrapper shell if its command line contains X; this has killed the calling command more than once. Use[X]bracket patterns or match descendants of a known parent.- Commands that talk to a dead X server block forever; always wrap X probes in
timeout. - The server sudo password does not work on the local desktop (a failed
sudo -Sthere just burns an attempt; proven 2026-07-16 installing php-cli). For local root, go straight to the clipboard hand-off. - A fixed scratch file like
/tmp/claude-out.txtis shared with whatever the user ran last. Before trusting it, check that its contents (and mtime) come from your command; on 2026-07-19 it held unrelated SSH logs. A unique file name per run avoids the question. - Tools the user installed may not be on the agent shell's PATH (a PHP dev server was running while
which phpfailed in the agent shell). Look for the binary (ls /usr/bin/php* /snap/bin/*) before concluding it is absent.
See also
- Automating KDE Plasma 5 from a shell (qdbus, global shortcuts, taskbar pins, idle inhibit)
- VictusPop, Justin's desktop (hardware, OS, boot, quirks)
- "Mouse and keyboard dead" on X11: frozen display vs stuck grab
Linked from
- A calculator engine that gets 0.1 + 0.2 and sin(30) right
- Automating KDE Plasma 5 from a shell (qdbus, global shortcuts, taskbar pins, idle inhibit)
- Driving the Android emulator headlessly for QA
- Heredoc, stdin and scripted-edit pitfalls
- Shell and process gotchas for agent sessions on the desktop
- VictusPop, Justin's desktop (hardware, OS, boot, quirks)
- Working with Justin as an AI agent (ask first, confirm the target, precise verbs, artifacts over essays)