Files
w/README.md
T
Andreas c9c8714651 Add w(1) replacement for procps-ng 4.x
procps-ng 4.0 rewrote w to skip utmp entirely, leaving TTY blank,
IDLE identical for all users, and WHAT pointing at the sshd parent
process instead of the user's shell.

Reads utmp via who(1), idle via tty atime, and foreground process
group via the tpgid field in /proc/pid/stat.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-22 13:23:27 -04:00

57 lines
1.8 KiB
Markdown

# w
Drop-in replacement for `w(1)` on systems with **procps-ng 4.x**.
## Problem
procps-ng 4.0 rewrote `w` to read from `/proc` instead of utmp, but
the process→session→tty mapping is broken. On a typical SSH login you get:
```
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
andreas 192.168.10.11 12:52 2:17 0.00s ? sshd: andreas [priv]
andreas 192.168.10.11 12:52 2:17 0.00s ? sshd: andreas [priv]
```
Three bugs in one:
| Column | Bug | Root cause |
|--------|-----|------------|
| TTY | blank | utmp never read |
| IDLE | same for everyone | can't stat the tty without knowing its name |
| WHAT | shows `sshd: [priv]` | picks the SSH privilege-separation parent instead of the user's shell |
## Solution
This script reads the right sources directly:
- **utmp** via `who -u` — correct TTY name, login time, source host
- **`/dev/<tty>` atime** — real idle time
- **`/proc/pid/stat` `tpgid` field** — kernel's own record of each tty's
foreground process group, used for JCPU/PCPU/WHAT (no `TIOCGPGRP` ioctl
needed, which would fail on terminals we don't own anyway)
## Example output
```
13:07:07 up 15 min, 3 users, load average: 0.05, 0.10, 0.09
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
andreas pts/0 192.168.10.11 12:52 1:54 0.0s 0.0s -bash
andreas pts/1 192.168.10.11 12:52 0s 37.5s 37.5s claude
stephan pts/2 192.168.10.165 13:02 1s 0.0s 0.0s -bash
```
## Install
```sh
cp w ~/bin/w
chmod +x ~/bin/w
```
Make sure `~/bin` appears before `/usr/bin` in your `PATH`.
## Requirements
Python 3.6+, `who`, `uptime` — all standard on any Linux system.
Tested on Ubuntu 24.04 (Noble) with procps-ng 4.0.4.