I need to run a script as root called root.py. I need to create notifications within the script. I made a separate script specifically for the notifications called notify.py.
Here is notify.py:
import pynotify
import sys
def notify(title, message):
n = pynotify.Notification (title,
message,
"notification-message-im")
n.show()
if __name__ == '__main__':
notify(sys.argv[1], sys.argv[2])
notify.py script works well with a user not as root:
python notify.py Title Message
But I need to call this script inside root.py, which is ran as root. It fails if I run it as root. So, I tried to for lack of better words, sign out as root by launching it as another user (I also replaced sudo with gksudo in the following):
xhost local:user
gksudo -u user notify.py Title Message
Won't work.
Any ideas?
btw: I had a similar problem with running gtk from root ran upstart scripts. Also, gvfs won't change attributes as root when as other user as sudo.
Direct copy and paste:
You won't be able to open a server on port 80 without root privileges, this is a restriction on the OS level. So the only solution is to drop root privileges after you have opened the port.
Here is a possible solution to drop root privileges in Python: Dropping privileges in Python. This is a good solution in general, but you'll also have to add
os.setgroups([])
to the function to ensure that the group membership of the root user is not retained.I copied and cleaned up the code a little bit, and removed logging and the exception handlers so it is left up to you to handle
OSError
properly (it will be thrown when the process is not allowed to switch its effective UID or GID):from here.
If you MUST regain root priviledges there's always policykit.
sux - wrapper around su which will transfer your X credentials http://fgouget.free.fr/sux/
This is a wild shot, please downvote it if it's wrong. Please next time, if possible, add the relevant error messages.
Maybe the root user just lacks a display to do GUI operations. Have you tried:
as root?.
as root, launch :
and run your notify program