Last active
September 28, 2017 12:34
-
-
Save rugk/8f90469c96b720641d7f438cf1bef863 to your computer and use it in GitHub Desktop.
Return currently active, graphical user…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # | |
| # THanks to https://unix.stackexchange.com/a/394763/146739, there are alos some alternatives | |
| # | |
| # returns all active users currently logged in | |
| getActiveUsers() { | |
| for sessionid in $(loginctl list-sessions --no-legend | awk '{ print $1 }'); do | |
| session=$( loginctl show-session "$sessionid" ) | |
| # filter not active users | |
| if ! echo "$session" | grep "State=active" >/dev/null; then | |
| continue | |
| fi | |
| if ! echo "$session" | grep "Active=yes" >/dev/null; then | |
| continue | |
| fi | |
| # filter remote users | |
| if ! echo "$session" | grep "Remote=no" >/dev/null; then | |
| continue | |
| fi | |
| # filter graphical users | |
| if ! ( echo "$session" | grep "Type=wayland" >/dev/null ) && ! ( echo "$session" | grep "Type=x11" >/dev/null ); then | |
| continue | |
| fi | |
| # show active user | |
| loginctl show-session -p Name --value "$sessionid" | |
| done | |
| } | |
| for user in $( getActiveUsers ); do | |
| # do something for each active user | |
| echo "$user" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment