LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to capture gnome-screensaver event doesn't work if run as root (http://www.linuxquestions.org/questions/linux-general-1/script-to-capture-gnome-screensaver-event-doesnt-work-if-run-as-root-4175446109/)

bubuntu 01-17-2013 10:21 PM

Script to capture gnome-screensaver event doesn't work if run as root
 
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.

bigrigdriver 01-19-2013 10:52 AM

What error message do you get when run as root?

bubuntu 01-21-2013 11:51 AM

None. It just doesn't capture the screen saver start and stop events. Suddenly today it doesn't capture the events even if I run as me.

bubuntu 01-22-2013 05:48 AM

Just discovered that gnome-screensaver isn't running. Now I have to find out why. It has always started automatically as far as I know, but now it doesn't. Did an update just before I noticed the problem, maybe that had something to do with it. Will keep digging as time permits.


All times are GMT -5. The time now is 06:50 PM.