Cron job Dbus error

I am trying to create a cron job on my Lubuntu 20.04 desktop to alert me if there is a change in the brightness value.

I have set up the rc.local file to reduce the brightness to half on startup like so:

echo 125 > /sys/class/backlight/amdgpu_bl0/brightness

But sometimes the brightness value automatically reverts back to 255.

So here is my script:

from plyer import notification
import os

if not 'DISPLAY' in os.environ:
    os.environ['DISPLAY'] = ':0'

brightnessfile = "/sys/class/backlight/amdgpu_bl0/brightness"

f = open(brightnessfile, "r")
brightness_value = f.read()
f.close()

brightness_value = brightness_value.strip()

if __name__=="__main__":

    if brightness_value > "125":
        notification.notify(
                    title = "Brightness Alert",
                    message = "Current brightness: " + brightness_value 
                )

This works as expected when executed from the terminal but when I try to execute the same script via cron, I am getting the following error:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name 'org.freedesktop.Notifications': no such name

During handling of the above exception, another exception occurred:

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files

And this is my cron command:

* * * * * ~/Documents/brightness/venv/bin/python3 ~/Documents/brightness/test.py >> ~/Documents/brightness/events.log 2>&1

It seems to be hardware-specific. Is Brightness adjusted to 64 Bit ?!
Or is there a chip somehow “smaller” ?

I think we need to see the full traceback, not just the final error
message. But my guess is that the error comes from the call to

notification.notify

since nothing else in your script looks like it could call out to an
external dbus library.

So my guess here is that it is an issue with the plyer library, either
an outright bug (“doesn’t work under cron”) or an issue where you need
to set the environment up correctly.

And that probably requires knowledge of dbus and Linux, in particular
the differences between the environment under a normal user and the
environment under cron.

There are many such differences, I’m not an expert sys admin but I think
these are the important ones:

  • under cron stdout and stderr will not be a tty, they may be a temp
    file or not exist at all; stdin will probably be /dev/null;

  • your .bashrc etc files aren’t run, so many environment variables
    may be different, or not set at all, including $PATH;

  • the working directory may not be $HOME;

  • the script may be run as a different user with different permissions.

If the notification is done with a GUI, then I don’t think it will work
under cron, because it is not an X environment.

Here is my hardware information:

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          4
On-line CPU(s) list:             0-3
Thread(s) per core:              1
Core(s) per socket:              4
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           24
Model name:                      AMD Ryzen 3 3300U with Radeon Vega Mobile Gfx
Stepping:                        1
Frequency boost:                 disabled
CPU MHz:                         1222.463
CPU max MHz:                     2100.0000
CPU min MHz:                     1400.0000
BogoMIPS:                        4192.22
Virtualization:                  AMD-V
L1d cache:                       128 KiB
L1i cache:                       256 KiB
L2 cache:                        2 MiB
L3 cache:                        4 MiB
NUMA node0 CPU(s):               0-3
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Full AMD retpoline, IBPB conditional, STIBP disabled, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht 
                                 syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid
                                  aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand
                                  lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw skinit wdt tce to
                                 poext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb hw_pstate sme ssbd sev ibpb vmmcall fsgsba
                                 se bmi1 avx2 smep bmi2 rdseed adx smap clflushopt sha_ni xsaveopt xsavec xgetbv1 xsaves clzero irperf
                                  xsaveerptr arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilt
                                 er pfthreshold avic v_vmsave_vmload vgif overflow_recov succor smca

Here is the full traceback:

Traceback (most recent call last):
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 177, in activate_name_owner
    return self.get_name_owner(bus_name)
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 361, in get_name_owner
    return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
    reply_message = self.send_message_with_reply_and_block(
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name 'org.freedesktop.Notifications': no such name

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/human/Documents/personal/code/python/automation/brightness/test.py", line 18, in <module>
    notification.notify(
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/plyer/facades/notification.py", line 79, in notify
    self._notify(
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/plyer/platforms/linux/notification.py", line 44, in _notify
    obj = session_bus.get_object(_bus_name, _object_path)
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 241, in get_object
    return self.ProxyObjectClass(self, bus_name, object_path,
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/proxies.py", line 250, in __init__
    self._named_service = conn.activate_name_owner(bus_name)
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 182, in activate_name_owner
    self.start_service_by_name(bus_name)
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 277, in start_service_by_name
    return (True, self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
    reply_message = self.send_message_with_reply_and_block(
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files

Since you mentioned that this might be because of a bug in the plyer library, I tried using the notify2 library but it’s giving the same error.

Notify script:

import notify2
import os

if not 'DISPLAY' in os.environ:
    os.environ['DISPLAY'] = ':0'

brightnessfile = "/sys/class/backlight/amdgpu_bl0/brightness"

f = open(brightnessfile, "r")
brightness_value = f.read()
f.close()

message = "Current brightness: " + brightness_value 

if __name__=="__main__":

    if brightness_value > "125":
        notify2.init("Notifier")
        n = notify2.Notification("Brightness Alert",
                         message
                        )
        n.show()

Traceback:

Traceback (most recent call last):
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 177, in activate_name_owner
    return self.get_name_owner(bus_name)
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 361, in get_name_owner
    return self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
  File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
    reply_message = self.send_message_with_reply_and_block(
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NameHasNoOwner: Could not get owner of name 'org.freedesktop.Notifications': no such name

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/human/Documents/personal/code/python/automation/brightness/alert.py", line 18, in <module>
        notify2.init("Notifier")
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/notify2.py", line 102, in init
        dbus_obj = bus.get_object('org.freedesktop.Notifications',
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 241, in get_object
        return self.ProxyObjectClass(self, bus_name, object_path,
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/proxies.py", line 250, in __init__
        self._named_service = conn.activate_name_owner(bus_name)
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 182, in activate_name_owner
        self.start_service_by_name(bus_name)
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/bus.py", line 277, in start_service_by_name
        return (True, self.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
      File "/home/human/Documents/personal/code/python/automation/brightness/venv/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
        reply_message = self.send_message_with_reply_and_block(
    dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files

Edit:

I installed postfix and then ran the cron as root using the “sudo crontab -e -u root” command.

Now I am getting this error:

/bin/sh: 1: cannot create /root/Documents/personal/code/python/automation/brightness/events.log: Directory nonexistent

It looks all somehow humblesome. If this is with Lubuntu you could look link here :

The source code of bash command ‘xrandr’ could help you too, or ?