0

How can I list all screens launched with screen along with their CPU utilizations? (screen -ls gives the lists of screens but not their CPU utilization)

By CPU utilization of a screen, I mean the sum of CPU utilizations of the processes that was launched in the screen.

  • 1
    top and ps may be helpful. I don't know that what you want exists already. You might also try using awk to check through the output of ps -ef for all the descendent processes of each screen process separately, and then sum the CPU usage for each process tree. I wrote a similar script to check through PIDs and print all the ancestor processes for a given process. – Wildcard Jan 8 '16 at 22:11
  • 2
  • please clarify, if your question is specific to screen and please check if the suggestions from the linked question are applicable. If not, please go into detail why. – polemon Jan 8 '16 at 23:17
  • @polemon The question is specific to screen. I haven't found a solution using htop or ps, as suggested in the linked question. – Franck Dernoncourt Jan 9 '16 at 0:56
  • @drewbenn Thanks but I don't see anywhere in the question where the list the CPU utilizations for each screen is asked. – Franck Dernoncourt Jan 9 '16 at 1:03
2

This isn't a full answer, but I may update it later.

The following thrown-together one-liner, if put into a file called cpu_usage_for_pid.sh, will accept a PID as an argument and print the sum of the CPU usage percent of that PID and all of its child PIDs.

#!/bin/bash

awk '{sum += $2} END{print sum}' <(ps -o pid,pcpu p "$(pstree -p "$1" | awk -F '[^0-9][^0-9]*' -v thepid="$1" 'NR = 1 {printf "%d", thepid} { for (i=1; i<=NF; i++) { if ($i != "") { printf ",%d", $i }}}')")

Right now my VM has just 0% usage for all processes, so it's hard to test. But you run it like ./cpu_usage_for_pid.sh 1241.

This is a prototype, not for production use.

From The Art of Unix Programming, which is in turn quoting Mike Lesk:

...he'd lash together some combination of shell scripts and awk code that did roughly what was needed, tell the customers to send him some clerks for a few days, and then have the customers come in and look at their clerks using the prototype and tell him whether or not they liked it. If they did, he would say “you can have it industrial strength so-many-months from now at such-and-such cost”

:)

  • Thanks, I guess when combined with ps -aux | grep screen one should be able to get the list the CPU utilizations for each screen. – Franck Dernoncourt Jan 9 '16 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.