0

I need to put the CPU value of a particular pid into a variable to detect its value.

I am able to get the pid of the program by using pidof and I am able to get the CPU value by using:

ps -p <pid> -o %cpu

which gives me:

%CPU
14.8

But I do no want the %CPU part, just the value, to then put into a variable to then question whether it is above 90% or not.

Could you please offer any assistance?

Thank you.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
omega1
  • 1,031
  • 4
  • 21
  • 41

3 Answers3

3

Rename the header using ps' -o field=name syntax with an empty name:

cpu_percent=$(ps -p "$yourpid" -o %cpu=)

To test whether it's greater than some value, we can strip the decimals (bash can't handle them) and compare:

yourpid=$$  # Example using current shell's pid

cpu_percent=$(ps -p "$yourpid" -o %cpu=)
echo "Fractional percent is $cpu_percent"

cpu_percent=${cpu_percent%%.*}
echo "Integer percent is $cpu_percent"

if [ "$cpu_percent" -ge 40 ]
then
  echo "It's more than 40"
fi
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thank you. I have this code but it reports the same regardless of the value of cpu_percent #!/bin/bash cpu_percent=$(ps -p 3850 -o %cpu=) echo $cpu_percent if [ cpu_percent>40 ]; then echo "Do something" else echo "Don't do something" fi – omega1 May 08 '14 at 19:03
  • @omega1 There are many basic problems with that check (shellcheck points out several). I've added an example for how to do it. – that other guy May 08 '14 at 19:18
  • Great, thanks! This works, although I have discovered that (ps -p "$yourpid" -o %cpu=) does not seem to reflect the CPU usage I see in TOP for example, is there a reason for that? Thanks again for your help. – omega1 May 08 '14 at 19:26
  • @omega1 %CPU in `top` shows CPU usage in the last second. CPU% in `ps` shows CPU usage over the lifetime of the process. – that other guy May 08 '14 at 19:27
  • Oh no! I needed it in real time... Without wanting to make you curse me, is there any way to get the same value from `top` rather than from `ps`? Thanks again! – omega1 May 08 '14 at 19:31
1

Use tail to print the last line

ps -p <pid> -o %cpu | tail -n 1

If you want it in a variable, do this:

percent=$(ps -p <pid> -o %cpu | tail -n 1)

The $() syntax means "put the result of running the command inside the parentheses into a variable".

Then you can compare like this:

# Convert to integer
percent=${percent/.*/}
if [ $percent -gt 20 ]; then
   echo Greater
else
   echo Less
fi
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Hi, thanks Mark, but that line gives me a blank line: ps -p -o %cpu | awk '{print $2}' give me a blank line ps -p -o %cpu | awk '{print $1}' give me the original value with the %CPU above it Any idea why? – omega1 May 08 '14 at 18:33
  • Hi Mark, I think you got confused by the misformatting of the original question version. The ps output in fact consists of two lines. – Jonas Schäfer May 08 '14 at 18:36
  • @JonasWielicki: Indeeed! Thank you - I run OSX and the format is different and the OP's formatting was misleading! – Mark Setchell May 08 '14 at 18:39
  • Now that I have booted an Ubuntu virtual machine I see that the other solution is more elegant, and have upvoted it accordingly ! – Mark Setchell May 08 '14 at 18:42
  • Thanks Mark, nearly there, and to get it into an integer, as it can report a decimal? – omega1 May 08 '14 at 19:15
  • Updated to truncate decimal part. – Mark Setchell May 08 '14 at 19:22
0

To get ps not to print the header material (%CPU, in your case):

ps h -p <pid> -o %cpu

h option turns off headers

man ps for more info

This way, you don't have to use tail -n 1 as above.

clemep
  • 124
  • 11