View Full Version : Python: How to suspend remote server?

January 9th, 2011, 01:13 PM
Hi folks,

I wrote a Python-Script for my music-server, which checks wether music is played. If no music is played, the server is put to suspend via dbus.
This is the python-function for suspending:

def GeheSchlafen():
bus = dbus.SystemBus()
proxy_object = bus.get_object('org.freedesktop.UPower','/org/freedesktop/UPower')
pm = dbus.Interface(proxy_object,'org.freedesktop.UPowe r')

This all works fine...
BUT ONLY if the script is started during boot, or (afterwards) using VNC and terminal.

If I connect to the server via ssh, and then start the script, the server won't suspend. It gives back the following error:

Traceback (most recent call last):
File "/home/produnis/bin/SuspendIfQuiet.py", line 327, in <module>
File "/home/produnis/bin/SuspendIfQuiet.py", line 164, in GeheSchlafen
File "/usr/lib/pymodules/python2.6/dbus/proxies.py", line 68, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/pymodules/python2.6/dbus/proxies.py", line 140, in __call__
File "/usr/lib/pymodules/python2.6/dbus/connection.py", line 620, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.UPower.GeneralError: not authorized

So, I tryed to export the current dbus-session to the script.
I wrote another script, which is startet during boot and which saves the dbus-info into a file:

set | grep DBUS_SESSION_BUS_ADDRESS > ~/.DBUS_temp

...and then I wrote another script called "dbus.run.sh", which is thought to export the needed dbus-session, and which looks like this:

source /home/produnis/.DBUS_temp

So, now I start my original script with the following command:

dbus.run.sh MyScript.py

I still get this dbus-exception.

Is there any way to somehow "import" the dbus-session within my Python-Script?
Or does anyone know an alternative way to suspend the server via ssh and python?

January 10th, 2011, 10:41 AM
I modified the script to make sure that the correct dbus-adress is used:

f = open("/home/produnis/.DBUS_temp", "r")
session = f.readline()
session = session.replace('DBUS_SESSION_BUS_ADDRESS=','')
session = session.replace('\r','')
session = session.replace('\n','')
os.environ["DBUS_SESSION_BUS_ADDRESS"] = session

The var is correctly set.

BUT I still get that error...

org.freedesktop.UPower.GeneralError: not authorized

Does anyone know how to configure dbus-server in order to allow ssh-users to suspend the machine?

January 10th, 2011, 12:36 PM
I don't know too much about dbus, but your stacktrace indicates that it is a permissions error rather than syntax/code one.

Maybe it has something to do with your login rights with ssh.

If you find an answer, I'd be interested in knowing it.



January 11th, 2011, 08:20 PM
Yes, it runs as root at startup, and presumably not when you launch it from the shell. Try it with "sudo".