Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for a simple way to determine the number of currently logged in users, using only the filesystem and basic bash functions (instead of using binaries).

The uptime binary appears to be reading from /var/run/utmp according the its source code, but /var/run/utmp doesn't seem to update in real time (it shows tty7 and pts/1-5 for my user, but I'm only on tty7 and pts/5 at the moment). However, the uptime binary correctly shows "2 users".

Note: Instead of relying on uptime or getutent() in C, I'm trying to better understand how the underlying system works.

What's the best place to find a current, accurate number of users logged into the system?

share|improve this question
2  
The utmp man page contains some background information. In my opinion, it is not possible (or at least really painful) to interpret utmp files with only basic shell functions. There is also who which shows the current allocated terminals. –  jofel Feb 7 '13 at 9:34
    
I'm actually writing some Python to decode utmp, but it's still very painful. I'm trying to avoid relying on subprocess calls to existing binaries, and force myself to use the actual Linux filesystem contents to gather data in order to learn more about Linux/procfs/utmp. –  wroberts Feb 7 '13 at 22:44
add comment

1 Answer 1

You can see how many pty's are allocated by looking at /proc/sys/kernel/pty/nr, however that won't necessarily be how many people are logged in.

For instance, on one of my servers I am currently the only person logged in but there are 7 pty's in use because of users running screen.

share|improve this answer
    
This is very helpful - I'll play with it tonight. Thanks! –  wroberts Feb 7 '13 at 22:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.