You are not logged in.
Hi there!
This may be a simple question at first glance: who is connected and how many sessions per user are opened? I thought a 'who' or 'w' would do it.
Sadly, it will only work with TTY sessions. Any session started from a login manager for X will not be taken into account by any of the aforementioned commands.
'loginctl list-... ' does the trick for systemd-based systems. But I'm looking for a more portable solution (BSD, other GNU, etc.).
Any thoughts?
Last edited by Ambrevar (2014-01-11 08:51:00)
Offline
I'm not using a login manager, so I can't test, but what about 'users'?
Offline
Does who -a work?
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Nope, 'who -a' does not see the loginctl sessions. It will print available TTY when only loginctl has been used:
INLOGGNING tty1 2013-12-29 19:54 228 id=tty1
INLOGGNING tty2 2013-12-29 19:54 230 id=tty2
Offline
'users' looked promising... But same thing: only TTY are taken into account.
Offline
who for tty's.
ls -l /dev/pts for X consoles
Offline
ls -l /dev/pts lists the virtual terminals running on every X sessions. This is not exactly what I want. A user can be connected without having any terminal running. On the other hand a user can be connected only once and use several terminals on the same session. In both cases, the result will be wrong.
Last edited by Ambrevar (2014-01-03 15:49:33)
Offline
loginctl
EDIT: Apologies for not reading the first post carefully enough
Last edited by snakeroot (2014-01-10 14:21:36)
Offline
loginctl
From the first post:
'loginctl list-... ' does the trick for systemd-based systems. But I'm looking for a more portable solution (BSD, other GNU, etc.).
Offline
How about something like
ps -eo user,tty --no-headers | sort -u
(edit: added --no-headers)
Last edited by Trilby (2014-01-10 12:52:46)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thanks, Trilby, this seems to be a good lead!
I've been playing with 'ps' a little to see what I could get. First I think the following command is equivalent and more portable:
$ ps -ax -o user='' -o tty='' | sort -u
'-e' is GNU-specific, and --no-header can be avoided by setting all header entries to the null string.
This will display all 'users', even 'root', 'polkitd', 'dbus', etc. Every X terminal will add another superfluous entry, but we can get rid of it easily:
$ ps -ax -o user='' -o tty='' | grep -v ' pts/' | sort -u
To display a list of all users with the number of started sessions:
$ ps -ax -o user='' -o tty=''| sort -u | awk '$0 !~ / pts\// {x[$1]++} END{for(i in x) print i, x[i]}'
doe 2
polkitd 1
dbus 1
root 3
To display the number of started session for the specified user:
$ ps -o user='' -o tty='' -u $USER | sort -u | grep -cv ' pts/'
Do not use '-U $USER', since it will display one root entry per X session (X euid is $USER but X ruid is root).
I've not tested the above commands on BSD systems, but according to the 'ps' man page from FreeBSD, this should work well. But it may fail if the TTY are not reported the same way. It also relies on the pts names.
Don't know if anyone can come up with a better suggestion, but for now I'm quite satisfied with the result, so I'll mark it as a 'solved'. Thanks again!
Last edited by Ambrevar (2014-01-11 08:53:36)
Offline