How to get script execution time from within the shell script in Linux


Written by - Deepak Prasad

In my last article I shared examples to get script name and script path in a shell script, Now let me help you with some examples to find out the elapsed time in minutes using bash script. Bash script execution time. Get script execution time in shell script. How to get command execution time in Unix. bash script start end time. How to calculate script execution time in Unix. bash get command execution time.

There are two parts of this question.

  1. Get script execution time or command execution time externally
  2. Get script execution time internally

 

Get script execution time externally

You can use 'time' command to get the execution time of the script. This command will call the Tcl interpreter count times to evaluate script (or once if count is not specified). It will then return a string of the form 503 microseconds per iteration which indicates the average amount of time required per iteration, in microseconds.

# time /tmp/script.sh
Script Execution Time: 5.00 seconds

real 0m5.015s
user 0m0.005s
sys 0m0.014s

Here,
real or total or elapsed (wall clock time) is the time from start to finish of the call. It is the time from the moment you hit the Enter key until the moment the script execution completed.
user - amount of CPU time spent in user mode.
system or sys - amount of CPU time spent in kernel mode.

 

Get command execution time

Use the time command in the below format to get command execution time. At the end of execution the time command will give you the execution time values. You can further user awl/grep/sed to get the required details.

# time rpm -Uvh /export/home/iserver/linux/install/Linux/rhel7_64/Packages/atk-2.22.0-3.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:atk-2.22.0-3.el7                 ################################# [100%]

real 0m0.104s
user 0m0.077s
sys 0m0.027s

The output is same as explained above.

 

Get script execution time internally

Here it gets tricky as the time command can be only called externally but if you wish to get a script execution time from within the script then we have to put some function within the script.

Below I have created a dummy script to measure the script execution time internally

#!/bin/bash
# This should be at the top of the script to get the start time of the script
start=$(date +%s.%N)

# Here you can place your function
sleep 5

duration=$(echo "$(date +%s.%N) - $start" | bc)
execution_time=`printf "%.2f seconds" $duration`

echo "Script Execution Time: $execution_time"

Let us execute our script

# /tmp/script.sh
Script Execution Time: 5.00 seconds

So our script ran for 5 seconds.

But this is not a very reliable solution because if the script exits abruptly then the script will fail to print the execution time. So we have to take some extra measures to make sure in any case the script makes sure it gets the script duration.

But that would vary from script to script so I can not predict that here.

You can write some function for exit such as

# This should be at the top of the script to get the start time of the script
start=$(date +%s.%N)

function exit_with_error {

duration=$(echo "$(date +%s.%N) - $start" | bc)
execution_time=`printf "%.2f seconds" $duration`
echo "Script Execution Time: $execution_time"
exit 1
}

So now you can use exit_with_error function for all possible scenarios where your script could exit, so that you get script execution time properly for all success and failure scenarios.

 

Lastly I hope the steps from the article to check or get command execution time and to get the script execution time from within the shell script in Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Views: 16

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment