0

I'm running a python script as a daemon and want to query window information using xprop. This seems to only be possible if I run xprop as the same user as is logged in and started X locally. I've tried running the daemon as my user and it works, but it'd be more robust and work for other users if I could just run xprop as which ever user is currently logged in.

How do I get the name of the currently logged in user?

EDIT: I can't just read $USER or getpass.getuser() because the script may be running as root. I need the username of the guy who's running X.

I'm not sure if this means looking for the user owning display :0 as below (is it always :0?). What's the best way?

> who
myusername :0      2015-03-13 19:52
myusername pts/0   2015-03-13 19:53

Perhaps something along the lines of:

ps -o user -p `pgrep -n '^(startkde|gnome-session)'` | tail -1

although clearly this depends on the window manager.


The answer here suggesting os.getlogin() seems promising but doesn't work because it "returns the name of the user logged in on the controlling terminal of the process" and thus fails with OSError: [Errno 25] Inappropriate ioctl for device from my daemon.

Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • you mean this `who | awk '$2==":0"{print $1}'` ? – Avinash Raj Mar 13 '15 at 10:11
  • @orlp I don't believe this is a duplciate. Your linked question asks for the current user and I've described why this is not what I want. – jozxyqk Mar 13 '15 at 10:27
  • 1
    The "new and shiny" method to do this is probably to ask the session manager via dbus for the owner of the primary seat. – dhke Mar 13 '15 at 11:10
  • @dhke I'm intrigued, particularly by new and shiny. I don't think I know enough to know what to search for. Do you have time to elaborate in an answer or point me in the right direction? – jozxyqk Mar 13 '15 at 11:15
  • 1
    The thing is, you probably shouldn't do this, but rather start a tiny *client service* with the user session that holds the information. If you really need information about the currently logged in user (of which there can be more than one), you might want to look at ConsoleKit seat API (http://www.freedesktop.org/software/ConsoleKit/doc/ConsoleKit.html#Seat) or *sigh* systemd-logind: http://www.freedesktop.org/wiki/Software/systemd/logind/. There is **no portable** way to what you want. – dhke Mar 13 '15 at 11:21

0 Answers0