Time Difference

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time Difference
# 1  
Old 03-30-2012
Time Difference

Is this way of finding the time difference a correct way of doing it or is it error-prone.

#****Check if lastrun exist. If exists check if the difference is 1 hour or not and act accordingly***********************
if [ -f $LOG_DIR/lastrun ]; then
	lastrun_time=`cat $LOG_DIR/lastrun`
	curr_time=`date +%s`

	#Here we are checking if the last run was within the last hour, if true we simply exit without running.	
	if [ $((curr_time-lastrun_time)) -le 3600 ]; then
		#echo "File is being run within an hour of last run.exiting."
		#Removing last run, if the last run is beyond an hour we remove the file and rerun the grep.
		rm -f LOG_DIR/lastrun


Last edited by sreekanthragi; 03-30-2012 at 04:34 AM..
# 2  
Old 03-30-2012
$(($curr_time - $lastrun_time))

Please use code tags.
# 3  
Old 03-30-2012
What does $LOG_DIR/lastrun contain ?

Actually, POSIX compliant shells should not require dollar signs when using integers in an arithmetic expansion:

If the shell variable x contains a value that forms a valid integer constant, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.
Shell Command Language: 2.6.4 Arithmetic Expansion
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-30-2012

lastrun contains the time when the script was last run, the same as `date +%s`

# 5  
Old 03-30-2012
If it contains epoch time then I would think this is a valid way of finding the difference.

I noticed a $-sign is missing
rm -f LOG_DIR/lastrun

It is a good practice to use double quotes around variable reference:
rm -f "$LOG_DIR/lastrun"

Although I am sure the path to your log dir will not contain spaces or funny characters..

You might like to use something like:
exit 1

So you have a return code..

Last edited by Scrutinizer; 03-30-2012 at 05:24 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Time difference between two time stamps

Hi Friends, I have 2 varaibles which contain START=`date '+ %m/%d/%y %H:%M:%S'` END=`date '+ %m/%d/%y %H:%M:%S'` i want the time difference between the two variables in Seconds. Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

2. Programming

time difference in MySql

Hi All I want the time difference from current date time For eg. From current date time i want to subtract 10 hrs and after subtracting i want the resultant in date time in MySql (1 Reply)
Discussion started by: parthmittal2007
1 Replies

3. SCO

Time difference

Need some help please. I am running SCO Openserver 5.07 on a Windows 2003 Server with VMware Server 1 If I run # ps -ef|grep /etc/cron the date that it shows the cron process started is older than the date I get from running the uptime command. In other words it looks like the date... (2 Replies)
Discussion started by: wjace
2 Replies

4. 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

5. Shell Programming and Scripting

Time difference

Hi, I have a tab delimited file with GMT time. How to convert the GMT to PST time and store the line only if date falls to 2 days ago date. Say today is 16, date should be of 14. or else remove the line abc - - efg - - hij - - kln - - ... (10 Replies)
Discussion started by: sandy1028
10 Replies

6. Shell Programming and Scripting

Time Difference

Hi Experts... I want to calculate the time difference between two date-time values (using ksh). It can return the difference in hours (or whatever..) For eg: time_diff "09/12/2009 12:30" "09/10/2009 12:30" should return 1464 hours... $time_diff "09/12/2009 12:30:00" "09/10/2009... (5 Replies)
Discussion started by: PRKS
5 Replies

7. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies

8. Shell Programming and Scripting

Time difference

Hi All, I know there has been a lot of things that have been written about date arithmetic, but perhaps I have missed something.. The following script takes the input from a file name fail.txt with the following format: CLASSDB 20060328122808 CPPARMS 20060814222056 Where $1 is a file name... (4 Replies)
Discussion started by: Segwar
4 Replies

9. UNIX for Advanced & Expert Users

time difference

Hi, i have one hard coded time which will be 23:45 and one will be sysdate (same date) and time less than 23:45. i want to start my job at 23:45 and the input file will be arriving before that. so i want to make sure that the task starts only at 23:45 and from the input file time till 23:45... (3 Replies)
Discussion started by: decci_7
3 Replies

10. Shell Programming and Scripting

Time difference calculations

Hi All.. Does anyone have a useful function where I can enter two date/timestamps and it calculates the difference in time in hours, minutes and seconds between the 2? Any feedback much appreciated. :D Kind Regards Satnam (1 Reply)
Discussion started by: satnamx
1 Replies
Login or Register to Ask a Question

Featured Tech Videos