Skip to content
Advertisement

Get name of users(persons, not applications) in linux system using psutil library

I am trying to use psutil library to get users logged in to linux system.

For that i used function psutil.users()

>>> import psutil
>>> psutil.users()
[suser(name='vibhcool', terminal='tty2',host='localhost',started=1473815296.0)]

I want to extract the username from this list, what shall i do? Also what is suser here?

Advertisement

Answer

I don’t know why they choose the name suser, but it’s actually a namedtuple.

That shouldn’t matter, you get the name of a user like so:

>>> import psutil
>>> users = psutil.users()
>>> first_user = users[0]
>>> name = first_user.name
>>> print(name)
'vibhcool'

In short:

>>> import psutil
>>> print(psutil.users()[0].name)
'vibhcool'
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement