How do I get the username owning the currently active gui session?

蓝咒 提交于 2020-01-06 02:24:09

问题


I published some scripts to automatically mount and unmount disks in Linux, and now I got a request to make the disks owned by the currently logged in user. That is, the user currently holding the gui occupied (we don't care about terminal users in this case).

How would I go about getting hold of the username? There could be a multitude of different window managers, so different solutions depending on wm might be needed, if no wm-independant solution exists.

For a start, a solution just targeting Gnome might be enough.

The script is running outside of any user session (triggered by udev).


回答1:


If you are running a bash script, the username of the currently logged-in user should be stored as an environment variable, $USER or $USERNAME. The env command will give you a list of all environment variables in the current shell.

Now if you want the user id of the current user, use the id command.




回答2:


w -hs | grep tty7 | cut -f1 -d ' ' seems to work if your xserver is attached to tty7 (modify accordingly).




回答3:


Gnome should start an process called gdm (Gnome Display Manager).

If you grep for it, it should pass you the user who is running it.

ps aux | grep gdm 


来源:https://stackoverflow.com/questions/17996707/how-do-i-get-the-username-owning-the-currently-active-gui-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!