Signal when there are n users logged in via SSH
by Turbocapitalist from LinuxQuestions.org on (#4TZ27)
I'm looking for a simple way to signal when there are n or more users logged into a particular system via SSH. In particular I want to trigger actions when the threshold is passed in either direction. It is easy to trigger an action as root via PAM when someone logs in or out but that by itself is full of race conditions and so I am wondering about the logic required to prevent race conditions with a lot of concurrent users and as well as some people logged into the same account multipe times.
Should I just try to keep a single persistent file containing a counter that is incremented and decremented on login and logout? Or is there a better way. A shell script is preferable.
The following environment variables are available to PAM:


Should I just try to keep a single persistent file containing a counter that is incremented and decremented on login and logout? Or is there a better way. A shell script is preferable.
The following environment variables are available to PAM:
- DBUS_SESSION_BUS_ADDRESS
- LANG
- PAM_RHOST
- PAM_SERVICE
- PAM_TTY
- PAM_TYPE
- PAM_USER
- PWD
- SSH_AUTH_INFO_0
- XDG_RUNTIME_DIR
- XDG_SESSION_CLASS
- XDG_SESSION_ID
- XDG_SESSION_TYPE