CALCULATE THE TIME TAKEN FOR A JOB OR TASK TO COMPLETE IN LINUX

Use ‘time’ command to measure the elapsed time of a job to complete from start till end

[oracle@orcl19x ~]$ time cat sale1.csv > /tmp/sale1.csv

real    0m1.994s
user    0m0.000s
sys     0m0.113s

In time output ,there are three output which is often confused.

To be concise,

  • real – wall clock time including all the processes running in background (CPU + wait time)
  • user – Time spent by foreground user process out of kernel
  • sys – Time spent in the kernel by the system

You can also use below shell script to calculate the elapsed time manually to measure for a job or command or task to complete

[oracle@orcl19x ~]$ cat elatime.sh
#!/bin/bash


ela_s=$(date +%s)
vmstat 5 10
ela_e=$(date +%s)

ela_t="$(($ela_e - $ela_s))"
echo "Total elapsed time: $ela_t sec"

Sample output:

[oracle@orcl19x ~]$ ./elatime.sh
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 4  0   5556 247632      0 2455676    0    0   287   536  138  236  2  1 92  6  0
 0  0   5556 247632      0 2455708    0    0     0    11  105  199  0  0 100  0  0
 0  0   5556 247616      0 2455708    0    0     0     6   97  197  0  0 100  0  0
 0  0   5556 247616      0 2455708    0    0     0     6   87  176  0  0 100  0  0
 0  0   5556 247616      0 2455708    0    0     0     3   74  152  0  0 100  0  0
 2  0   5556 247260      0 2455704    0    0     0     6  128  190  1  5 94  0  0
 0  0   5556 247260      0 2455704    0    0     0     7   77  174  0  0 100  0  0
 0  0   5556 247268      0 2455828    0    0    18    36  105  217  0  0 99  1  0
 0  0   5556 247268      0 2455828    0    0     0     6   93  190  0  0 100  0  0
 0  0   5556 247268      0 2455828    0    0     0     6   81  184  0  0 100  0  0
Total elapsed time: 45 sec

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s