0

I'm writing a simple script that should periodically change the brightness of all the screens in the system. I figured out that for this I can use following command:

xrandr --output {display name} --brightness {float in a range [0.0,1.0]}

First I wanted to use xbacklight, but it turns out that it cannot control all the screens brightness, so I switched to xrandr.

When I run this command from the terminal it works fine, but next I tried to schedule it to a crontab to run it periodically, first it wasn't working at all, but then I found that I need to provide $DISPLAY env variable to cron so that the script will know where to look for X.

It seems to be working now, however not totally fine, because sometimes it gives error like this:

X Error of failed request:  BadAccess (attempt to access private resource denied)
  Major opcode of failed request:  131 (XInputExtension)
  Minor opcode of failed request:  57 ()
  Serial number of failed request:  18
  Current serial number in output stream:  19 

And I cannot get why this error occurs and what to do in order to fix it?

Here's the content of my crontab file:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/user
DISPLAY=:0
XAUTHORITY=/home/user/.Xauthority

0 * * * * python3 /home/user/script.py > /home/user/scriptLog 2>&1

And the script (it runs every hour, and at first turns off the keybord and all screens for 5 minutes and then it turns them on):

#!/usr/bin/env python3

import time
import os
import sched

SEC_PER_HOUR = 60 * 60
MIN_PER_HOUR = 60
SEC_PER_MIN = 60
DEVICE_ACTIVE = 139
dur = 5

def xinput_set_prop(devices, prop, val):
    for id in devices:
        os.system("xinput set-prop {} --type=int --format=8 {} {}".format(id, prop, val))

def xrandr_set_brightness(devices, val):
    for id in devices:
        os.system("xrandr --output {} --brightness {}".format(id, val)) 

cur_t = time.time()

if cur_t / SEC_PER_HOUR - cur_t // SEC_PER_HOUR < dur / MIN_PER_HOUR:
    keyboard_ids = [line.replace("\n", "") for line in os.popen("""xinput list | sed -rn 's/.*id=//; s/\s+.*slave\s+keyboard.*//p'""")]
    monitor_ids = [line.replace("\n", "").split(" ")[-1] for line in os.popen("""xrandr --listmonitors""")]

    xrandr_set_brightness(monitor_ids, 0.01)
    xinput_set_prop(keyboard_ids, DEVICE_ACTIVE, 0)

    run_t = cur_t // SEC_PER_HOUR * SEC_PER_HOUR + dur / MIN_PER_HOUR + dur * SEC_PER_MIN
    del_t = run_t - cur_t

    s = sched.scheduler(time.time, time.sleep)
    s.enter(del_t, 1, xinput_set_prop, argument=(keyboard_ids, DEVICE_ACTIVE, 1))
    s.enter(del_t, 1, xrandr_set_brightness, argument=(monitor_ids, 1.0))
    s.run()

I'm using Ubuntu 16.04, with the Unity desktop.

York's
  • 11
  • Not the way you are doing it now but there is a cron based script that periodically changes brightness of the display: https://askubuntu.com/questions/894460/automatically-adjust-display-brightness-based-on-sunrise-and-sunset/894470#894470 – WinEunuuchs2Unix Dec 07 '17 at 00:06
  • @WinEunuuchs2Unix thanks, but I'm more asking about why can I receive the error sometimes? Why it looks like I receive it randomly? What does it mean? And how can I try to resolve it? – York's Dec 07 '17 at 00:23
  • @dessert could you please elaborate on what exactly was wrong with the initial formatting? At the moment I'm not getting it, and don't want to repeat the mistake. – York's Dec 07 '17 at 10:27
  • The scripts works fine when you start it in a terminal, isn't it? Please try whether you can reproduce the error when you run it as root (with sudo python3 …) . – dessert Dec 07 '17 at 12:13
  • @dessert yeah from terminal it works fine. Just tried it with sudo it seems to reproduce the error. – York's Dec 07 '17 at 12:17
  • 2
  • @pa4080 I don't see how it can be duplication of the given questions. In the first one problem was resolved by adding $DISPLAY env variable, which in my case is added and I'm still getting randomly the error. While the second one is about how to use crontab to launch GUI script, which I also do. And still have an error some times. – York's Dec 07 '17 at 13:16
  • @York's, try to add $DISPLAY env programmatically as it is shown in my suggestion, it is not :0 each time. Also I think probably the value of $XAUTHORITY is incorrect due a typo in my suggestion is shown how to get this value programmatically. – pa4080 Dec 07 '17 at 13:23
  • @pa4080 initially I was adding them programmatically, but after receiving error for few times, I tried to simplify the script and just took values from running the env for these variables, though it didn't help a lot, as I still receive error sometime. What is the typo in $XAUTHORITY ? – York's Dec 07 '17 at 13:26
  • @York's: According $XAUTHORITY, I don't know: Are you sure the part /user/ is correct in your actual corn job? Are you running the job from the user's crontab? Could you check who is the owner of /home/<user>/.Xauthority when you receive the error message? – pa4080 Dec 07 '17 at 13:35
  • @pa4080 the /home/user/.Xauthority belong only to user, I run script from the user's crontab, /user/ is correct in my actual cron job. And $USER variable is not defined when running the cron job. – York's Dec 07 '17 at 13:54
  • Unfortunately the script doesn't produce any result on my system and I can't test it. – pa4080 Dec 08 '17 at 10:20
  • @pa4080 if you want to test it you should start it in minutes range between 0 and 4, because it turns off the keyboard and sets brightness to be low only if it has started at range [0..4] minutes and at minute 5 it should return everything back. – York's Dec 08 '17 at 10:56

0 Answers0