1

My system (Ubuntu 15.10) has a bug: I have enabled "Require my password when waking from suspend" on the "Brightness & Lock" settings panel, but it does NOT ask for my password, which is incredibly insecure.

As a workaround, I'm trying to write a script which will live under /etc/pm/sleep.d. It should be something like this:

#!/bin/bash

case "${1}" in
  hibernate)
    # Do nothing
    ;;
  resume|thaw)
    su -c "gnome-screensaver-command --lock" MYUSERNAME
    ;;
esac

Problem is, this doesn't work; the gnome-screensaver-command fails with the following message:

** Message: Failed to get session bus: Could not connect: Connection refused

Then, I tried to change the command to:

su MYUSERNAME -c "export $(dbus-launch) && gnome-screensaver-command -l"

Which then fails with:

** Message: Failed to get session bus: The connection is closed

So, the question is: what would be the right way to do this?

dsetton
  • 363
  • Not sure, but this might help: https://askubuntu.com/questions/457204/how-can-i-lock-the-screen-using-the-new-lockscreen-from-the-command-line/492963 – Fern Moss Jan 17 '16 at 22:23

2 Answers2

2

You need to export DBUS_SESSION_BUS_ADDRESS of the remote machine in your ssh before using dbus commands

You have to find it locally on the machine to which you want send commands remotely by typing the command:

set | grep DBUS

This will give you the shells current DBUS_SESSION_BUS_ADDRESS value which you should export in your ssh shell after connecting to the remote machine and then enjoy.

techraf
  • 3,316
sandman
  • 21
  • 2
  • Hi! A couple of points: 1) there is no remote machine. This is all one machine (my own, local). I never mentioned a remote machine in my question. 2) "You need to export DBUS_SESSION_BUS_ADDRESS" --> this is exactly what the second attempt does: export $(dbus-launch). This does not work. – dsetton Mar 21 '16 at 13:27
0

I was able to solve this by reading the right DBUS_SESSION_BUS_ADDRESS from the current gnome session. Full example:

dbus_address=$(xargs -n 1 -0 < /proc/`pgrep gnome-session`/environ | grep DBUS)
su USERNAME -c "export $dbus_address && gnome-screensaver-command -l"
Joe
  • 101