0

I have read similar posts here, here, here and here that mentioned some environment variables DISPLAY and DBUS_SESSION_BUS_ADDRESS. Setting them at the top of my user crontab enabled notify-send to work in my user crontab. However, the exact same crontab does not work if set in the sudo crontab -e, why not? And how can I make it work?

test.sh

#! /bin/bash
env > $1
notify-send "I want to see this"

crontab -e

SHELL=/bin/bash
PATH="/usr/bin:/home/ripytide/scripts/"
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DISPLAY=:0                                                                     
* * * * * test.sh /home/ripytide/crontab.env

sudo crontab -e

SHELL=/bin/bash
PATH="/usr/bin:/home/ripytide/scripts/"
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DISPLAY=:0                                                                     
* * * * * test.sh /home/ripytide/sudocrontab.env

crontab.env

SHELL=/bin/bash
PWD=/home/ripytide
LOGNAME=ripytide
_=/usr/bin/env
HOME=/home/ripytide
LANG=en_GB.UTF-8
USER=ripytide
DISPLAY=:0
SHLVL=1
PATH=/usr/bin:/home/ripytide/scripts/
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

sudocrontab.env

SHELL=/bin/bash
PWD=/root
LOGNAME=root
_=/usr/bin/env
HOME=/root
LANG=en_GB.UTF-8
USER=root
DISPLAY=:0
SHLVL=1
PATH=/usr/bin:/home/ripytide/scripts/
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
1
  • @Kamil Maciorowski That sounds promising, I assume I need to set the USER variable to my user "ripytide", after doing that it does not work unfortunately.
    – ripytide
    May 7 '21 at 22:40
0

D-Bus checks whether UIDs of the calling process and the session daemon are the same. Your script needs to run notify-send as the target user. If you insist to run the script as root then in the script you need sudo -u user notify-send ….

Keep in mind sudo sanitizes the environment, so DBUS_SESSION_BUS_ADDRESS from the environment of the script will not get to the environment of notify-send (/etc/sudoers and the security policy may allow sudo to keep the variable but in your case this probably won't happen by default).

You may try to change the settings, I won't elaborate. There is also sudo -E (see man 8 sudo). The least intrusive method is to request sudo to set the variable on demand:

sudo -u user DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
   notify-send "I want to see this"

When the shell processing the script expands $DBUS_SESSION_BUS_ADDRESS, the command becomes:

sudo -u user DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus \
   notify-send "I want to see this"

and this form can be as well used directly, so it's up to you.

In general things may be configured not to allow sudo to set the variable even this way. In the worst case the following should work:

sudo -u user sh -c '
   DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "I want to see this"
'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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