I am trying to
run scripts when gnome-screensaver starts or stops in Ubuntu. If I set up the scripts as described it works fine. I can't get it to work as root.
Here is the script ssTriger copied from the referenced web site with a small change (used "call" instead of "Popen" to invoke the scripts to run at start and stop of screen saver activation):
Code:
#!/usr/bin/env python
from gobject import MainLoop
from dbus import SessionBus
from dbus.mainloop.glib import DBusGMainLoop
from subprocess import Popen
from subprocess import call
class SSTrigger:
def __init__(self):
DBusGMainLoop(set_as_default=True)
self.mem='ActiveChanged'
self.dest='org.gnome.ScreenSaver'
self.bus=SessionBus()
self.loop=MainLoop()
self.bus.add_signal_receiver(self.catch,self.mem,self.dest)
def catch(self,ssOn):
if ssOn == 1: #Screensaver turned on
# call(["echo", "'opt/ssTrigger/ssStart'"])
call(["sudo", "/opt/ssTrigger/ssStart"])
#Popen(["/opt/ssTrigger/ssStart"])
# call(["sudo", "ifconfig","eth0","down"]) #works if ifconfig set in sudoers
else: #Screensaver turned off
# Popen(["/opt/ssTrigger/ssStop"])
# call(["echo", "'opt/ssTrigger/ssStop'"])
# call(["sudo", "ifconfig", "eth0", "up"]) #works if ifconfig set in sudoers
call(["sudo", "/opt/ssTrigger/ssStop"])
SSTrigger().loop.run()
ssStart is:
Code:
#!/bin/bash
echo "start"
#sudo ifconfig eth0 down
ifconfig eth0 down
exit
ssStop is:
Code:
#!/bin/bash
echo "stop"
#sudo ifconfig eth0 up
ifconfig eth0 up
exit
Both ssStart and ssStop are owned by root. These and ssTriger (owned by me) are in /opt/ssTrigger as per the referenced web page I got all this from.
Also have added this to /etc/sudoers so I don't have to enter my password when the scripts run.
Code:
user_me ALL=(ALL) NOPASSWD: /opt/ssTrigger/ssStart
user_me ALL=(ALL) NOPASSWD: /opt/ssTrigger/ssStop
I'm running ssTrigger from a terminal window and it works flawlessly but when I run it as root it won't capture the screen saver start or stop event. If I could do this then I could get rid of the scripts and "call(["sudo", "ifconfig", "eth0", "down"])" instead.
This is a potential solution to a question I posted a while back where I want to
put internet connection to sleep if no user activity but haven't had time to try and solve till now.
Any help will appreciated.