0

I have an assignment in bash scripting trying to measure cpu time used for a process passed into the script by name. I can find the process id and pass it to the top program in bash. However, I haven't figured out how to extract the cpu time from the top program. for example:

top is printing out:

top - 00:57:07 up  6:06,  2 users,  load average: 0.46, 0.31, 0.55
Tasks:   1 total,   1 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.7 us,  0.8 sy,  0.0 ni, 94.6 id,  0.9 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem:   1928720 total,  1738072 used,   190648 free,    57184 buffers
PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
3337 amarkovi  20   0  372m  31m  10m R   0.7  1.7  13:28.74 chromium-browse 

all I want from this is the TIME+ field to be assigned to variable so I can add up the time and print it out by it self.

I am a noob to bash scripting so please be patient.

thanks,

2 Answers 2

1

Do you have to use top? It should be much simpler (once you work out the right options) to use ps to give you just the fields you want, then use grep to select just the processes you want.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't have to use top. I can ps but I really don't understand the output modifiers and how to take the time value into a variable. I am a programmer and this shell options are very confusing. is used ps -p (pidof md) it gives me print out of time. What are the modifiers to use in order to only display time and assign the value to variable.
got that ps -o time -p pid now I need to cut it and save it :)
^^ skip header, using ps -otime= -p pid
0

Since it's an assigment i don't want to spoil all the fun :D. I'll just point you to some commands of which can help you in your endeavour : sed, awk and cut. With this 3 you can solve it in many ways, enjoy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.