How to check the execution time of your bash script?

Do you have a long running bash script on your server’s cron list? And you really wanna get notified about the total execution time for your cron?

Yeah, here we go! We can not find out the total execution time from the cron log. We can use bash variables to find out the total execution time.

However, you can check the cron execution status from the log /var/log/cron. One of my friends asked me about this question. He wanted to find out the total execution time of his cron job.

I did some search on Google and found out a solution by using SECONDS builtin variable available in Linux Bash. It tracks the number of seconds that have passed since the shell was started.

You need to set this variable 0, before starting the script. The value returned after some work is equal to the sum of assigned value (here 0) and the total time spend just before we called the SECONDS variable again.

I am using the “seq” command to generate a sequence and save it to a file. And we can check the time for cron execution using SECONDS builtin variable.

The sample code in a cron is pasted below:

File : /root/diff2.sh

#!/bin/sh
echo " " > /root/time.txt
echo "Start time is: " $(date +%T) >> /root/time.txt
SECONDS=0
seq 100000000 > /root/seq.txt
echo "End time is: " $(date +%T) >> /root/time.txt
duration=$SECONDS
echo "Total time spend is pasted below:"
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed." >> /root/time.txt

Cron sample:

[[email protected] ~]# crontab -l
*/5 * * * * /bin/sh /root/diff2.sh

See the output here:

[[email protected] ~]# cat /root/time.txt

Start time is:  20:20:01
End time is:  20:21:16
Total time spend is pasted below:
1 minutes and 15 seconds elapsed.

One more

[[email protected] ~]# cat /root/time.txt

Start time is:  00:30:01
End time is:  00:31:16
Total time spend is pasted below:
1 minutes and 15 seconds elapsed.

If you wish, you can set email alert for this cron.

There are many other options available to check the total time spend for a script. Please comment here, if you get a quick way than the one mentioned here 🙂

Please try it and let me know if you have any questions. Thanks for your time!!

Read also:

, ,

Post navigation

Arunlal Ashok

Operations Engineer at Endurance International Group. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com
Please follow the LinkedIn page

10 thoughts on “How to check the execution time of your bash script?

  1. Hi, nice script but there will be problem if you run it, for example at 11:59PM and something takes more than 1 minute 😉

  2. Hello sir , I m fresher and I want to ask one question that how to check kernel version without command line ? Is there any option

  3. Hello Bro, Is there a way to check the idle time of processes using PID? I launched the eclipse and its not been touched for another 10 mins. Shall we get this time ?

Leave a Reply

Your email address will not be published. Required fields are marked *