Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux cpu usage

Tags:

linux

I am working on unix. I want to knwo the current cpu usage of a process. I understood that ps give the average of cpu used till the process is up - it is not the current usage.

Is there a way to print only the cpu from the top command without 10 more parameters and headers? I know how to do it with awk - this is not the way i want to do it.

top  -p 20705 -bc -n 1  | tail -n 2 | awk '{ print $9}' | head -n 1

If there is another simple way to do it, not reading /proc/stat...

If there is a simple way doing it from c++, it is also ok.

like image 792
yaron Avatar asked Aug 25 '26 09:08

yaron


1 Answers

Most likely, you will need to read /proc/stat, However, here is an interesting article with C code that may help you out. To understand and use the output from the program you should do man 5 proc. And here is the source code.

The bottom line is that you will need to read from /proc/stat to do what you want.

like image 92
Chimera Avatar answered Aug 26 '26 23:08

Chimera