Linux – How to tell number of active users logged into a Django site

apache-2.2lamplinux

Is there a way to find the number of users that are currently logged into a Django site running on Linux? I need to restart the server and I want to make sure there are 'few enough' people that it won't be too much a hassle.

Best Answer

To see what users are logged into Linux, you can use w or who.

The output of w on my machine:

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
wesley tty1 :0 Tue12 39:54m 1:34m 0.06s pam: gdm-password
wesley pts/1 :0.0 Tue16 1:38m 0.45s 0.44s ssh user@remoteserver1.com
wesley pts/3 :0.0 Tue16 1:38m 0.49s 0.47s ssh user@remoteserver2.com
wesley pts/5 :0.0 Tue20 0.00s 0.25s 0.03s w

The output of who on my machine:

wesley   tty1         2012-02-14 12:28 (:0)
wesley   pts/1        2012-02-14 16:24 (:0.0)
wesley   pts/3        2012-02-14 16:34 (:0.0)
wesley   pts/5        2012-02-14 20:46 (:0.0)

To see which users are logged into an application, such as WordPress or Joomla, will require different, application specific methods, however.


Since you're using Django, you will need to programmatically query the session model for non-expired sessions. Check out the Q/A titled "How to get the list of the authenticated users?" over on StackOverflow for a more thorough explanation.