0

I am running a cron job with @ reboot bin/sleep 60 /path/to/script. I know the job runs 60 seconds after reboot but I get an error. The script is:

#!/bin/sh -e
sudo xrandr --newmode "2560x1440_60.00"  312.25  2560 2752 3024 3488  1440 1443 1448 1493 -hsync +vsync
sudo xrandr --addmode HDMI-1 2560x1440_60.00
sudo xrandr --output  HDMI-1 --mode 2560x1440_60.00

The error output is:

Can't open display
Can't open display
Can't open display

The script is supposed to reset the screen output resolution as I am accessing the Pi4 via VNC on my 5K 27" iMac. When I run the script from the terminal via VNC it just does everything fine. I should mention that due to my setup I do not get a pwd challenge when using sudo so when I just run the script from the CLI it runs and changes the display output without pwd challenge. What can I do to make this work in the cron job? Many thanks for any help! Hans

1
  • Why do you want to use a cron when it is simpler to just set default resolution in config.txt?
    – Milliways
    Apr 19, 2021 at 21:52

1 Answer 1

1

How does the cronjob know what display this refers to? Cron does not inherit any X11 environment.

The display that is used by X11 is found in the variable DISPLAY. For example, I might get:

$ echo $DISPLAY
localhost:10.0

But in your cronjob, $DISPLAY is empty.

A cronjob is not the place where you do this.

If the set-up is specific for your session, you woud typically put it in ~/.xinitrc.

1
  • Thank you very much for your answer. I didn't know that a cronjob can't figure out what display I am using. I am obviously very new to this so I find it difficult to understand why a script run manually vs. cronjob should have different "knowledge" of the environment. In any case, I'll investigate ~/.xinitrc. I have tried /etc/rc.local and other options I found online to run a script at boot but it seems that most options run too early and there is even less "awareness" of the environment.
    – HaWi
    Apr 19, 2021 at 17:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.