Getting the Start, End time and duration using date command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the Start, End time and duration using date command
# 1  
Old 11-01-2012
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


Code:
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 ?
# 2  
Old 11-01-2012
Code:
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); } '

This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-01-2012
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.
Code:
 
$ 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.

# 4  
Old 11-02-2012
i am using HP-UX ..... i tried same code but its throwing error

Code:
 
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 :

Code:
 
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.

# 5  
Old 11-02-2012
Anymore suggestions ?
# 6  
Old 11-02-2012
HP-UX date command does not have a -d option. You can check this thread for more options.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting week start date and end date based on custom period start dates

Below are my custom period start and end dates based on a calender, these dates are placed in a file, for each period i need to split into three weeks for each period row, example is given below. Could you please help out to achieve solution through shell script.. File content: ... (2 Replies)
Discussion started by: nani2019
2 Replies

2. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

3. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

4. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

5. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

6. Shell Programming and Scripting

Start time/end time and status of crontab job

Is there anyway to get the start time and end time / status of a crontab job which was just completed? Of course, we know the start time of the crontab job since we are scheduling. But I would like to know process start and time recorded somewhere or can be fetched from a command like 'ps'. ... (3 Replies)
Discussion started by: thambi
3 Replies
Login or Register to Ask a Question