top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find out CPU usage and memory consumption for a particular process? [CLOSED]

+2 votes
718 views

I have to write a python script which has to give CPU usage for particular process.
Ex: Lets say i have a process ABC which is running continuously for 10 hours.(Process may have 'n' number of threads.)
So,
1st) My script should have to check the CPU usage and Memory consumption for process ABC, and also for threads of it after every 5 seconds.

2nd) Script has to take an average of CPU usage and memory consumption and it has to print final result.

i have tried using this command ,

top -bn1 | grep "Cpu(s)" | \
           sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
           awk '{print 100 - $1"%"}'

Can anyone help in find out?

closed with the note: Problem got solved by top command
posted Jul 8, 2015 by Chirag Gangdev

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button
Assume you know the pid of the process then
ps -p <pid> -o %cpu,%mem

To get the PID of the process
ps aux | grep -i <process name> | awk {'print $2'}
Thank you sir
Sir, I have checked above command it is working properly.
but What if -p option is not available?
I have another board which is busybox (broadcom). where it is saying that "-p" option is not available.

As of now i am doing with "top -bn1" command and then parsing the string to get CPU% and MEM consumption
Is there any other option?
Try top -p <PID>
Yes, it seems right with other system, but in one of the board(BusyBox) -p is not available.
seems your system does not have -p option, try "top | grep <Process Name>" ....
Yes, its working perfectly.
Thank you

Similar Questions
+2 votes

I have a process in Linux which can have different memory footprint, I want a program or shell script which can print the time of the day when the memory usage is highest for this process.

Can someone help me.

+2 votes

We develop embedded software for 32-bit micros using Windows as the development platform.

We are seeking a general purpose scripting language to automate certain tasks, like cleaning out certain directories of certain types of files in preparation for ZIP'ing, generating certain source files automatically, etc.

Selection criteria:

a)Should be able to compile the script interpreter as a monolithic executable (no .DLL dependencies, etc.) for easy versioning and distribution of the script interpreter. (Note that I'm not asking that the script be a single executable, just the interpreter. To run a script you'd need both the script and the interpreter. The script would be a text file, and the interpreter would be a single .EXE.)

b)Should be extensible, in that one could add commands or library functions to the script interpreter in C (for efficiency), and the whole script interpreter could again consist of a single executable with no other dependencies. (Note that I'm not asking that the script be a single executable, just the interpreter. To run a script you'd need both the script and the interpreter. The script would be a text file, and the interpreter would be a single .EXE.)

c)Should be able to spawn compilers and capture the output, do file I/O, and all the other minor expected stuff.

d)Graphical capability would be nice.

I know that Tcl/Tk would do all of the above, but what about Python or any other alternatives?

+1 vote

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

...