How to find the PID(s) of a hung app in macOS from the shell (Terminal)? Within Activity Monitor.app it's easy to visually identify a hung app by seeing its name  in red text:

How can such hung app statuses be identified from the shell (Terminal.app) and the PID(s) of such processes retrieved?



UPDATE (The answer below linked to a great post, but doesn't yet contain the entire answer):

First step appears to be to identify the hung process by using spindump:

sudo spindump -notarget 5  -timelimit 60 -stdout -noFile -noProcessingWhileSampling   -aggregateStacksByProcess | grep -B 8 "Unresponsive for"


I will now need to extract out the PID (probably trivial) and this should do it. It would be nice if there were a more effective method... ie, it would be nice if we could discover how Activity Monitor itself is really doing this so quickly / effectively...
This is actually not straightforward to do, but other people have run into this and written codes you could use. See here.