1

I have a bash script which is very long and performs some stuff in the background (without producing any output).

I would like to get to know which lines/parts take the most of execution time.

Can I achieve with some kind of tool/function/linux builtin ?

Patryk
  • 22,602
  • 44
  • 128
  • 244
  • 1
    See http://stackoverflow.com/questions/5014823/how-to-profile-a-bash-shell-script – Stefan Apr 16 '13 at 12:05
  • Somehow it won't help. On redhat 6 I do not have `/etc/bash.bashrc` only `/etc/bashrc`. Even though if I use it instead of the former I am not getting the trace. – Patryk Apr 16 '13 at 12:15
  • You have to put the lines in the bash script you want to profile. – Stefan Apr 16 '13 at 12:24

2 Answers2

0

I can think of two approaches here.

1) Log the time each command or function starts at and output to a log file

# echo "$(date) : Started function_x" >> $LOGFILE

2) Figure out the number of seconds each function/task takes and log that.

startTime=$(date +%s)
longRunningFunction
endTime=$(date +%s)
echo "longRunningFunction took $(expr $endTime - $startTime) seconds" >> $LOGFILE 
-1

When I've had to do this in the past I've used this kind of format: ( time echo "hello" ) 2>>output.txt

Do this for every line in your script to time each one.

James
  • 3,957
  • 4
  • 37
  • 82