0
dbus-monitor --system --profile interface=org.freedesktop.login1.Session,type=signal,member={Lock,Unlock} |
  egrep --line-buffered -o 'Lock|Unlock' |
  while read SIGNAL; do
    case "${SIGNAL}" in
      Lock)
        for i in "$(systemd-path user-shared)/watchlock/lock.d/"*; do
          "${i}"
        done
        ;;
      Unlock)
        for i in "$(systemd-path user-shared)/watchlock/unlock.d/"*; do
          "${i}"
        done
        ;;
    esac
  done

I've made this script and I want to execute it as a regular user but dbus-monitor only works as root. Is there a way to execute dbus-monitor as root and the rest of the pipes as a regular user? Adding sudo before dbus-monitor is not an option as it is just a workaround, I could intend to execute the script as root and it would escalate privileges once again for no reason. I would also want to make use of polkit, pkexec for example. Thanks!

e142804
  • 3
  • 1

1 Answers1

0

Just like this:

cmd_a | sudo -u "${USER}" cmd_b

If the script is started as root, cmd_a will run as root and cmd_b will run as $USER.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • > Adding `sudo` before `dbus-monitor` is not an option as it is just a workaround, I could intend to execute the script as root and it would escalate privileges once again for no reason. – e142804 Feb 14 '20 at 09:05
  • Yeah, just read this. In case the script runs as root, you may do the other way around `cmd_a | sudo -u "$USER" cmd_b` – hek2mgl Feb 14 '20 at 09:06
  • https://superuser.com/questions/1255613/should-i-use-sudo-in-a-script-or-sudo-an-entire-script – e142804 Feb 14 '20 at 09:07
  • You are making contradicting comments here. I've first suggested that, to only run `cmd_a` as root, but you said _that's not an option_ – hek2mgl Feb 14 '20 at 09:09
  • Oh, forgot my thought. I want to escalate as less as possible, and in a way that would enable me to use polkit. – e142804 Feb 14 '20 at 09:12
  • Then I suggest to use `sudo cmd_a | cmd_b` and run the main script as a normal user – hek2mgl Feb 14 '20 at 09:13
  • Okay, you know how to persuade. Thanks. – e142804 Feb 14 '20 at 09:17