0

I am interested in finding out the last time a process was scheduled to run. That is, how long it has been sleeping, since it last ran. When was it last active? Basically, a generalization of what the w command prints, but for all processes, and not just the login shells.

To make this concrete: the w command prints a list of user sessions, when they logged in, and how long they've been idle. For example:

$ w 17:17:04 up 10 days, 17:35, 2 users, load average: 0.28, 0.26, 0.30 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT linas tty7 :0 31Jan25 10days 13:27m 2.09s mate-session linas pts/7 10.70.70.2 16:54 7:08 0.07s 0.07s -bash 

This says that linas logged in on pts/7 twenty-three minutes ago (17:17 minus 16:54) and did some typing at the keyboard, burning a trivial amount of CPU time, and then did nothing at all for seven minutes and eight seconds. In the kernel, the process bash is in interruptible sleep, waiting for keyboard input, and has been like that for 7:08 mm:ss.

How can I get this info for all processes? Or, if that's not available, then all ptys? So, pts/1 though /6 also have bash attached to them, but they're ... idle. No one is typing into them, right now.

It seems like the ps command should be able to show this, but I can't figure it out. Perhaps this info is in /proc/<pid> but if so, not clear where. The GPT/LLM chatbots all cluelessly provide nothing but wrong answers (insisting on ps -o etime which is something else entirely.)

1 Answer 1

3

The kernel doesn't keep this information for processes, it's not available.

The w command gets this from the timestamp on the terminal the user is logged into. For terminals that are in use but not a login session, you can use stat (1 or 2) on each pty to get the same information w displays.

It would also be technically possible to poll all processes for used cpu time and look for ones where this number is not increasing. But even then, a process that just grabs a few cycles every now and then might appear idle when it is not, and the cpu time used won't go up fast enough to appear in ps.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.