0

I need to know the complete execution time, CPU and memory utilization of a shell script that I have written. I want to execute the commands that outputs the CPU and memory utilization from the same script. I am more interested in real time CPU utilization rather than average time CPU utilization when the script finishes its execution.Is this feasible? OS - FreeBSD and linux

Please help.

Abhishek
  • 73
  • 1
  • 8
  • 1
    You can get the execution time with the `time` util. Try `time sh test.sh`. – Kirill Rogovoy May 29 '17 at 07:33
  • 1
    How frequently do you need to see the instantaneous cpu utilization? If you just need to know it on demand, typing Control-T (on freebsd) may be enough. – Mark Plotnick May 29 '17 at 11:24
  • Hi Mark, Thanks for your reply, I need to know the exact cpu utilization when the script finishes its execution.So if the script runs for say 10 seconds then what the CPU % it took during 10 seconds? – Abhishek May 30 '17 at 05:03

1 Answers1

0

What I understood from your question is you want to know how much CPU is been utilized by your script. Then just after running your script capture the process id of that script/processes and use ps -p -o %cpu,%mem command to see the CPU utilization

  • Thanks for your reply Anupam, my script hardly runs for 20-25 seconds, I could not get the process id using "top", I even tried assigning the process name to my script but couln't do so. Any pointers will be helpful. – Abhishek Jun 07 '17 at 08:02
  • Ohk. Just add ps -ef| grep script name in your file at second line. This will capture the process id and add this command with ps -p pid -o %cpu,%mem. Then use & at the end of script to make it run in back end. – Anupam Singh Jun 09 '17 at 01:50