I've searched everywhere and found nothing about this. I'm trying to create a small gauge that shows the CPU usage of one process specified by PID.

I need the command to print a simple answer , hence why top -p $pid isn't good. The command is executed by PHP and needs to print the response immediately.

Something that is closely related to what I need is

sudo ps -p $pid -o %cpu

But after testing and searching some more I found out this prints the average usage since the process started. I need the real time usage from the moment the command si ran, like it shows on top.

Is this possible?


$cmd = 'sudo sh -c "top -n1 | awk \'/30100/ {print $9}\'"';
echo exec($cmd);

tried without shell

$cmd = "sudo top -n1 | awk '/30100/ {print $9}'";
echo exec($cmd);

still no result, it doesn't echo anything


Well, I would start off with the naive approach like so:

$ top |grep %pid

On ubuntu 12.04, it outputs the line for the PID once per update of top:

    username@usrver / $ top |grep 2593
 2593 username   20   0 1103m 170m  16m S   30  2.2  67:25.38 chrome             
 2593 username   20   0 1103m 170m  16m S   28  2.2  67:26.24 chrome             
 2593 username   20   0 1103m 170m  16m S   30  2.2  67:27.15 chrome             
 2593 username   20   0 1103m 170m  16m S   23  2.2  67:27.86 chrome             
 2593 username   20   0 1103m 170m  16m S   25  2.2  67:28.60 chrome

Then grab the 10th field with awk, although, we don't really need to use grep here. awk can regex for your PID, then print the CPU value:

username@usrver / $ top | awk '/2593/ {print $10}'
  • Works like a charm when I type it into the terminal, but PHP doesn't print anything. Am I doing something wrong? edited OP with some snippets – Aron H. Apr 10 '15 at 12:10
  • Well, i'm not really a PHP programmer, but it looks like you're echoing the return value of exec($cmd), rather than capturing the output of $cmd. php.net/manual/en/function.exec.php – ernesto ricks Apr 13 '15 at 0:13
  • @AronH. see my answer below – Vishal Aug 11 '15 at 12:21

Though the question is already answered and accepted, I think its incomplete. There are a few points missing from the accepted answer.

  1. You need to run top in batch mode using -b if you are running it as a child process and want to grab any meaningful output. If not it will have a mix of control characters as a result of running in interactive mode.

  2. Specify the number of iterations you want, using -n, otherwise it will result in infinite iteration and the child process will never finish. You need to have at least two iterations, so that top will have a reference point to collect the difference, which is the actual CPU usage.

  3. Specify the interval between iterations using -d. On most systems, the default value is 3 seconds. So if you want a per second usage, you need to specify it explicitly.

Now combining all the above points, you get

top -b -n 2 -d 1

As we have two iterations, we need to consider the second output only. Use awk to get the second output, which starts with string top.

top -b -n 2 -d 1 | awk '/^top/{i++}i==2'

Now lets filter out the PID, say 2593 and print the 9th column, which contains the CPU usage. Only consider output lines that starts with PID.

top -b -n 2 -d 1 | awk '/^top/{i++}i==2' | awk '/^\s*2593/ {print $9}'

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.