I need to write a bash script which will be able to return elapsed time, RAM usage and CPU usage of executed script.

my script:

STARTTIME=$(date +%s%N)

#here is script which needs to be executed

ENDTIME=$(date +%s%N)

echo Elapsed time: $(($(($ENDTIME - $STARTTIME))/1000000))ms

Elapsed time works, but how to do the rest?

  • Hello and welcome to the U&L stack exchange site! Please review the Help Center to get information on how to best post on this site.To get to your question, where are you having the most trouble? What have you tried and what specifically does not work? Please edit your post to include this additional context. Thank you. – kemotep Oct 19 '18 at 16:49

Look for the /usr/bin/time (GNU time) and its supported formatting options that you could do something like:

$ /usr/bin/time -f 'Elapsed time: %es\nMemory usage: %M KB\nCPU usage: %P' echo 'Hello World!'
Hello World!
Elapsed time: 0.02s
Memory usage: 672 KB
CPU usage: 57%

see man time for more formatting options.

  • please mention that that (GNU) time command will have to be explicitly installed; at least on debian and centos, it's not installed by default. (there is a /usr/bin/time on *bsd, but it doesn't support any formatting options). – mosvy Oct 19 '18 at 17:50

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.