5

I have below script and for a requirement I have to place some function for each of these script to get time information for each script and at last show total time.

My main scripts looks like below:

/u01/scripts/stop.sh ${1} | tee ${stop_log}    
/u01/scripts/kill_proc.sh ${1} | tee ${kill_log}    
/u01/scripts/detach.sh ${1}| tee ${detach_log}   
/u01/scripts/copy.sh ${1} | tee ${copy_log}   

I want to use something like below function to get every script execution time and at last with a global variable I can show Total time taken by all the scripts.

I created below but unfortunately I could not use properly , if you have something kindly help here.

time_check()    
{         
  export time_log=${log}/time_log_${dts}.log          
  echo 'StartingTime:'date +%s > ${time_log}       
  echo 'EndingTime:'date +%s >> ${time_log}           
}          

I want to use something like above function to get every script execution time and at last with a global variable I can show total time taken by all the scripts . Could anyone please guide how to get the desired result.

codeforester
  • 39,467
  • 16
  • 112
  • 140
user2500742
  • 721
  • 1
  • 5
  • 10

1 Answers1

2

If you are OK with the time granularity of seconds, you could simply do this:

start=$SECONDS
/u01/scripts/stop.sh ${1} | tee ${stop_log}
stop=$SECONDS   
/u01/scripts/kill_proc.sh ${1} | tee ${kill_log}
kill_proc=$SECONDS   
/u01/scripts/detach.sh ${1}| tee ${detach_log}
detach=$SECONDS  
/u01/scripts/copy.sh ${1} | tee ${copy_log}
end=$SECONDS

printf "%s\n" "stop=$((stop-start)), kill_proc=$((kill_proc-stop)), detach=$((detach-kill_proc)), copy=$((end-detach)), total=$((end-start))"

You can write a function to do this as well:

time_it() {
  local start=$SECONDS rc
  echo "$(date): Starting $*"
  "$@"; rc=$?
  echo "$(date): Finished $*; elapsed = $((SECONDS-start)) seconds"
  return $rc
}

With Bash version >= 4.2 you can use printf to print date rather than invoking an external command:

time_it() {
  local start=$SECONDS ts rc
  printf -v ts '%(%Y-%m-%d_%H:%M:%S)T' -1
  printf '%s\n' "$ts Starting $*"
  "$@"; rc=$?
  printf -v ts '%(%Y-%m-%d_%H:%M:%S)T' -1
  printf '%s\n' "$ts Finished $*; elapsed = $((SECONDS-start)) seconds"
  return $rc
}

And invoke it as:

start=$SECONDS
time_it /u01/scripts/stop.sh ${1} | tee ${stop_log}    
time_it /u01/scripts/kill_proc.sh ${1} | tee ${kill_log}    
time_it /u01/scripts/detach.sh ${1}| tee ${detach_log}   
time_it /u01/scripts/copy.sh ${1} | tee ${copy_log}
echo "Total time = $((SECONDS-start)) seconds"

Related:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • Thanks for your quick suggestion. I will check your approach. Also if any function we can place that is main requirement . In function if we can put any global variable that can be used to show the result at last . – user2500742 Aug 11 '18 at 16:07
  • Great . Thanks for the help , it seems it will fulfill my requirement . I will check this and update further. Thanks, – user2500742 Aug 11 '18 at 16:37
  • Hi , I just tried the function . I got something like below for printf line 23: printf: `(': invalid format character – user2500742 Aug 11 '18 at 17:36
  • Check your bash version. Use the first function if you have < 4.2 version. – codeforester Aug 11 '18 at 17:37
  • Acknowledged . I have lower version version 4.1.2(2)-release – user2500742 Aug 11 '18 at 17:42
  • I have a doubt , if scripts failed in some stage say in detach.sh , I have to start it next phase say copy.sh then in that case how this will calculate ? shouldn't we have a global variable to call that and preserve the time till detach phase . Please advise me if we can do something like that . – user2500742 Aug 11 '18 at 17:47
  • Calculation will work regardless of if the timed command fails or not. You need additional logic if you want special treatment for failures. – codeforester Aug 11 '18 at 17:53
  • Please guide how to handle failures , failures are bound to happen. – user2500742 Aug 11 '18 at 18:11
  • That’s a separate question – codeforester Aug 11 '18 at 18:45
  • 1
    Ok , understood . This function working fine for me but it is not supporting incase of failure in any steps . I will create another question. Thanks for all your help. – user2500742 Aug 12 '18 at 01:12
  • I modified the function so that it does return the exit code of the command it runs. – codeforester Aug 12 '18 at 04:24