Getting the Start, End time and duration using date command

Oracle Enterprise Linux

We want to track how long a process takes to complete its execution. This is what we want in the schell script

Before the process is started , get the time with date, hours and minutes

execute the process

After the process has ended , get the time with date, hours and minutes


## Print the following 3 things:
Job started at :
Job ended   at :
It took  x hours and x minutes to complete this job.

Any idea how this can be done ?

START_DATE=`date`
# Execute Process
END_DATE=`date`

echo "Job started at: $START_DATE"
echo "Job started at: $END_DATE"

sdt=$( date -d "$START_DATE" +%s )
edt=$( date -d "$END_DATE" +%s )

echo "$edt $sdt" | awk ' { printf("It took %.2f hours to complete this job.\n", ($1-$2)/60/60); } '
1 Like

Thanks Bipinajith. I tested your script with sleep for 2 minutes. The results are not quite what I wanted. I didn't get the minutes part right.
But I'll work on it. Thank you.

 
$ echo $0
-ksh
$ cat test.sh
START_DATE=`date`
sleep 120
END_DATE=`date`
 
echo "Job started at: $START_DATE"
echo "Job started at: $END_DATE"
sdt=$( date -d "$START_DATE" +%s )
edt=$( date -d "$END_DATE" +%s )
echo "$edt $sdt" | awk ' { printf("It took %.2f hours to complete this job.\n", ($1-$2)/60/60); } '
$ ./test.sh
Job started at: Thu Nov  1 23:53:38 HKT 2012
Job started at: Thu Nov  1 23:55:38 HKT 2012
It took 0.03 hours to complete this job.

i am using HP-UX ..... i tried same code but its throwing error

 
START_DATE=`date`
sleep 30
END_DATE=`date`
echo "Job started at: $START_DATE"
echo "Job started at: $END_DATE"
sdt=$( date -d "$START_DATE" +%s )
edt=$( date -d "$END_DATE" +%s )
echo "$edt $sdt" | awk ' { printf("It took %.2f hours to complete this job.\n", ($1-$2)/60/60); } '

error :

 
Start_End_time_calculation.sh
Job started at: Fri Nov  2 14:51:09 mal 2012
Job started at: Fri Nov  2 14:51:39 mal 2012
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]
It took 0.00 hours to complete this job.

Anymore suggestions ?

HP-UX date command does not have a -d option. You can check this thread for more options.