0%
April 30, 2025

Count Execution Time of a Shell Script

shell

Helper Functions

start_timer
start_time=0

start_timer() {
  echo "Starting $1..."
  start_time=$(date +%s)
}
end_timer
end_timer() {
  local end_time=$(date +%s)
  local duration=$((end_time - start_time))

  # Format the time nicely
  if [ $duration -ge 3600 ]; then
    local hours=$((duration / 3600))
    local minutes=$(( (duration % 3600) / 60 ))
    local seconds=$((duration % 60))
    echo "$1 completed in ${hours}h ${minutes}m ${seconds}s"
  elif [ $duration -ge 60 ]; then
    local minutes=$((duration / 60))
    local seconds=$((duration % 60))
    echo "$1 completed in ${minutes}m ${seconds}s"
  else
    echo "$1 completed in ${duration}s"
  fi
}

Apply the Functions

start_timer "DB clone"

... # many logic here

end_timer "DB clone"