XRandR script runs correctly from command line, fails as cron job

cronlxdexrandr

I'm running LXDE on Fedora 21. My script's purpose is to extend the display across two monitors:

#!/bin/sh
xrandr --output VIRTUAL1 --off --output LVDS1 --mode 1440x900 --pos 1280x124 --rotate normal --output TV1 --off --output VGA1 --mode 1280x1024 --pos 0x0 --rotate normal

This runs without issue from a terminal window but chokes as a cron job.

From my cron log:
Jul 9 20:14:01 localhost CROND[19494]: (user) CMD (/home/user/screens.sh)
Jul 9 20:14:01 localhost CROND[19492]: (user) CMDOUT (Can't open display )

Best Answer

xrandr needs the $DISPLAY variable set to tell it which X session it's manipulating, and that isn't being set in the cron environment.

xrandr could be working on your default local X session, or a second one that you started by running startx from a TTY, or a session to a remote display being forwarded over SSH, or a nested X session running inside another one using Xnest, etc. Without the $DISPLAY environment variable (or the --display command line argument) it can't know in general which session to connect to, so it bails out.

For example, the following command may resolve your issue:

DISPLAY=:0 /home/user/screens.sh
Related Question