0

Am using Iris Mini to filter the blue light at night, it works pretty well but having to execute it manually is annoying. So am trying to use cron to start it each night at 8PM.

This is what i have written executing crontab -e. The command works if i execute it in the terminal

Crontab

0 20 * * * sh /home/jogarcia/Software/open-iris-mini.sh

open-iris-mini.sh

#!/bin/bash

export DISPLAY=0:. /home/jogarcia/Software/iris-mini

I also executed xhost +localhost for testing (before the time of the cron tab).

Searching in the logs with grep CRON /var/log/syslog I found this lines that seems to suggest that is actually been executed:

cron log

Nov  2 20:00:01 my-computer-is-name CRON[8391]: (user) CMD (sh /home/jogarcia/Software/open-iris-mini.sh)

But it isn't working because i can't see the results on my screen (it should display a kind of orangish color) am really lost, I don't know what am doing wrong.

To see the errors I installed a mail service.

Local mail error

QXcbConnection: Could not connect to display 0:.
jogarcia
  • 105
  • Related, if not a dupe: https://unix.stackexchange.com/questions/10121/open-a-window-on-a-remote-x-display-why-cannot-open-display – Kusalananda Nov 03 '20 at 09:23

3 Answers3

1

This fails because cron jobs run in their own context, not the GUI context (where controlling colors "makes sense").

In order to get this to work, there are 2 steps

In your script, add a definition for the DISPLAY environment variable (used as a pointer to the X server). Something like

export DISPLAY=0:.

but check with echo "$DISPLAY" in your GUI context.

In your GUI startup (perhaps in $HOME/.config/autostart)

xhost +localhost

to tell the X server to accept connections originating from host localhost (or userid localhost -- X Windows security is wack). You have to do this EVERY GUI login.

Read man xhost X.

waltinator
  • 4,865
  • Am a Linux noob, i added what i think is your answer and still doesn't works. I updated my question would you check it? – jogarcia Nov 05 '20 at 01:29
  • 1
    That should probably be DISPLAY=:0 The format there is typically hostname:displaynumber, where hostname is optional. If you put it in your own crontab instead of root's, then xhost should not be needed as xauth will work. OP (@jogarcia) has (probably) done this correctly by using crontab -e as himself. The other trick here is to install an email system to get the error messages. – David G. Nov 05 '20 at 02:03
  • @DavidG. I installed a mail service, now i can see the error logs, it simply says that the cron job can't connect to the display – jogarcia Nov 05 '20 at 02:31
  • 1
    @jogarcia That would typically mean either DISPLAY isn't set right, or display security prevents connection (xauth and xhost). Try running your script in a command window. Try running it like env -uDISPLAY /home/jose/Software/open-iris-mini.sh (and if it says that file isn't executable, use chmod +x) – David G. Nov 05 '20 at 02:39
  • @DavidG. The file is running, though it's still throwing the error that it couldn't connect to the display – jogarcia Nov 05 '20 at 14:12
  • @DavidG. I got your command working – jogarcia Nov 05 '20 at 14:20
  • @DavidG. I got the cron job working, thanks bro. You should make your own answer. – jogarcia Nov 05 '20 at 14:22
1

The secret (as pointed out by @waltinator is to set DISPLAY correctly.

That should probably be DISPLAY=:0

The format there is typically hostname:displaynumber, where hostname is optional.

The second issue is display security. If you put the cronjob in your own crontab instead of root's, then xhost should not be needed as xauth will work. OP has (probably) done this correctly by using crontab -e as himself. (Note that $HOME must be correct for xauth security to work. This is part of what putting it in your own crontab does.)

Another trick here is to install an email system to get the error messages.

When OP reported (after installing an email system) "that the cron job can't connect to the display", it probably means that either DISPLAY isn't right or something went wrong with display security (xauth and xhost). To that, I advise to try running the script in a command window. Try running it like env -uDISPLAY /home/jose/Software/open-iris-mini.sh (and if it says that file isn't executable, use chmod +x)

(answer added per request of OP.)

David G.
  • 1,369
0

This is exactly what worked for me using the answers of both who answered before me

Use crontab -e for the cron job

MAILTO=somemail@mail.com
0 20 * * * sh /home/jogarcia/Software/open-iris-mini.sh

Then on the sh file:

#!/bin/bash

DISPLAY=:0 /home/jogarcia/Software/iris-mini

To see if it was working as intended with the display i used the command:

env -uDISPLAY /home/jogarcia/Software/open-iris-mini.sh 

To see the cron logs i installed postfix sudo apt-get install postfix, all local.

jogarcia
  • 105