Teo Gelles
Teo Gelles

Reputation: 82

How to calculate sum of CPU usage of users who are not me

I would like a bash command which outputs the sum of the cpu usage percentages of all users who are not me, including users logged in through ssh, screen, and other non-terminal sessions.

Upvotes: 0

Views: 422

Answers (1)

hauntsaninja
hauntsaninja

Reputation: 999

Here's a command that should do the trick:

ps ax -o pcpu:5,user --no-headers | tr -s ' ' | grep -v $(whoami) | cut -d' ' -f2 | (tr '\n' + ; echo 0; ) | bc

The ps command will list the CPU usages of every process alongside its owner. The tr will squeeze together multiple spaces, so the cut later on works as wanted. The grep will filter out processes owned by you. The cut command will select the first column, ie, the CPU usages. The tr will substitute new lines for plus signs, and finally bc will evaluate the resulting arithmetic expression.

Cheers.

Upvotes: 1

Related Questions