CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Users & Groups / w

w

Show who is logged in and what they're doing

Beginneruserssessionsload

w prints the uptime and load averages, then one line per logged-in user with where they came from, how long they've been idle, how much CPU their session used and the command they're running right now. It's who and uptime with process detail.

Syntaxw [-h] [-s] [-i] [-p] [USER]

Watch it run

A CPU alert fired on devops-lab. Before digging into services, check whether someone logged in is running something heavy.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
w

Load climbed over the last 15 minutes (2.03 to 6.12). rahul's session has used over 14 minutes of CPU, almost all in the script he started.

Anatomy of a command

$
commandw

Session and activity overview (procps).

How it works

The first line is the same as uptime: the current time, time since boot from /proc/uptime, the number of user sessions and the 1, 5 and 15 minute load averages from /proc/loadavg. For each session w gets the user, terminal, origin and login time from utmp (on newer systemd builds, from logind). IDLE is the time since the terminal device was last read or written. For the processes on that terminal it scans /proc: JCPU is the CPU time of all processes attached to the terminal, PCPU the time of the current foreground process, and WHAT is that foreground process's command line. That's how you can see that a user is running a heavy query or a tail -f without opening top.

Options you'll actually use

-hNo header (no uptime line, no column names)
-sShort format: drop LOGIN@, JCPU and PCPU
-iShow IP addresses instead of hostnames in FROM
-fToggle the FROM column
-pShow the PID(s) of the process in WHAT
-uIgnore the current username when working out the process (with su)
-oOld style: blank idle times under one minute
USERLimit the list to one user

Common mistakes

Reading the load average as a CPU percentage

Load is the average number of tasks running or waiting (including uninterruptible I/O waits). 4.0 on a 16-core box is light; on 2 cores it's overloaded.

Compare load with nproc, and use top or vmstat to see whether it's CPU or I/O wait
Killing whatever w shows in WHAT without checking

WHAT shows the foreground process on the terminal; it may be a migration or a production fix a teammate is running.

Use w -p to get the PID, inspect it with ps, talk to the user, renice if needed
Concluding that 'nobody is logged in' because w lists no users

w only shows terminal sessions. Cron jobs, services and non-interactive ssh commands never appear.

Use top or ps -eo user,pid,pcpu,cmd --sort=-pcpu to see what really uses the machine

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show who is logged in and what they are doing, without the header lines.

  2. 2

    Show only your own sessions in the short format.

  3. 3

    Compare the first line of w with the output of uptime. Then compute whether the load is high for this machine.

Related commands

whoList logged-in usersuptimeHow long the system has run, and its load averagetopWatch CPU, memory and processes livepsSnapshot the running processes
PreviouswhoNextgroups
Linux / Users & Groups / w

w

Show who is logged in and what they're doing

Beginneruserssessionsload

w prints the uptime and load averages, then one line per logged-in user with where they came from, how long they've been idle, how much CPU their session used and the command they're running right now. It's who and uptime with process detail.

Syntaxw [-h] [-s] [-i] [-p] [USER]

Watch it run

A CPU alert fired on devops-lab. Before digging into services, check whether someone logged in is running something heavy.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
w

Load climbed over the last 15 minutes (2.03 to 6.12). rahul's session has used over 14 minutes of CPU, almost all in the script he started.

Anatomy of a command

$
commandw

Session and activity overview (procps).

How it works

The first line is the same as uptime: the current time, time since boot from /proc/uptime, the number of user sessions and the 1, 5 and 15 minute load averages from /proc/loadavg. For each session w gets the user, terminal, origin and login time from utmp (on newer systemd builds, from logind). IDLE is the time since the terminal device was last read or written. For the processes on that terminal it scans /proc: JCPU is the CPU time of all processes attached to the terminal, PCPU the time of the current foreground process, and WHAT is that foreground process's command line. That's how you can see that a user is running a heavy query or a tail -f without opening top.

Options you'll actually use

-hNo header (no uptime line, no column names)
-sShort format: drop LOGIN@, JCPU and PCPU
-iShow IP addresses instead of hostnames in FROM
-fToggle the FROM column
-pShow the PID(s) of the process in WHAT
-uIgnore the current username when working out the process (with su)
-oOld style: blank idle times under one minute
USERLimit the list to one user

Common mistakes

Reading the load average as a CPU percentage

Load is the average number of tasks running or waiting (including uninterruptible I/O waits). 4.0 on a 16-core box is light; on 2 cores it's overloaded.

Compare load with nproc, and use top or vmstat to see whether it's CPU or I/O wait
Killing whatever w shows in WHAT without checking

WHAT shows the foreground process on the terminal; it may be a migration or a production fix a teammate is running.

Use w -p to get the PID, inspect it with ps, talk to the user, renice if needed
Concluding that 'nobody is logged in' because w lists no users

w only shows terminal sessions. Cron jobs, services and non-interactive ssh commands never appear.

Use top or ps -eo user,pid,pcpu,cmd --sort=-pcpu to see what really uses the machine

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show who is logged in and what they are doing, without the header lines.

  2. 2

    Show only your own sessions in the short format.

  3. 3

    Compare the first line of w with the output of uptime. Then compute whether the load is high for this machine.

Related commands

whoList logged-in usersuptimeHow long the system has run, and its load averagetopWatch CPU, memory and processes livepsSnapshot the running processes
PreviouswhoNextgroups