How is the “users” field of `uptime` calculated

uptime

For me, uptime yields 9 users, but

ps -Af | cut -f1 -d' ' | sort | uniq | wc -l yields 14.

I'm not exactly sure where the 9 is coming from.

Before I jump to conclusions though, please let me know if you guys do not have such a discrepancy.

Best Answer

You're comparing apples and oranges.

ps will list the processes running. You're then getting the count of unique process-owning user ids.

uptime will report the users logged on. By using utmp. More details at https://github.com/coreutils/coreutils/blob/master/src/uptime.c#L177

So, comparison of output, highlighting this, below.

# uptime
 16:52:37 up 30 days, 23:32,  1 user,  load average: 0.04, 0.04, 0.05
# w
 16:57:33 up 30 days, 23:37,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
steve    pts/0    cpc79909-stkp12- 16:50    5.00s  0.07s  0.28s sshd: steve [priv]
#


# ps -Af | cut -f1 -d' ' | sort | uniq | wc -l
7
# ps -Af | cut -f1 -d' ' | sort | uniq
chrony
dbus
polkitd
postfix
root
steve
UID
#
Related Question