Determine previous time in minutes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine previous time in minutes
# 1  
Old 08-08-2011
Determine previous time in minutes

I have several logs with where the time stamp in the logs are "YYYYMMDDHHMM".

I would like to check the last line in each file to make sure the entry is less than 5 minutes old.
My timezone is EST5EDT so the following will work for 1 hour. But I need something easy for 5 minutes ago.
Code:
STALE_TIME=$(TZ=$(date +%Z)+5; date '+%Y%m%d%H%M')

I am using ksh and do not have gnu or ksh94.

Is there an easy way I can find what the time was 5 minutes ago?

Example1: current time: 201108081607
stale time: 201108081602

Example2: current time: 201201010001
stale time: 201112312356

Last edited by Franklin52; 08-10-2011 at 10:35 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-08-2011
One possible solution is to place your script under cron to run every 5 minutes.

In the same script you can create a file with the current YYYYMMDDHHmm.

Every time you run your script, you read the only record in the timestamp file.

At the end, you overwrite it with the current date/time.
# 3  
Old 08-09-2011
I was hoping for something a little cleaner. This is an on demand script I am writing that will parse logs and display just the latest status. Any other ideas? Smilie
# 4  
Old 08-09-2011
Would using perl be an option?
# 5  
Old 08-09-2011
Quote:
Date arithmetic is always a pain
It is a bit easier when using EPOCH date format
date on Linux converts to EPOCH, but not on Solaris
For a portable solution, you need to do one of the following:
Install gnu date on solaris (already mentioned, needs human interaction to complete)
Use perl for the date part (most unix installs include perl, so I would generally assume that this action does not require additional work).
Date arithmetic in Unix shell scripts - Stack Overflow
# 6  
Old 08-09-2011
Quote:
Originally Posted by g.pi
Would using perl be an option?
Not sure but I can try. Do you have something in mind that I can try. I have never used perl.

I do think I did something a few years ago using the Julian date. I am goint to see if I can remember if that worked for me before or not.

---------- Post updated at 10:31 AM ---------- Previous update was at 10:05 AM ----------

Ok, I think it was cut time.

Code:
 
#!/bin/ksh
MIN_BACK=5
SEC_BACK=`expr ${MIN_BACK} \* 60`
CURRENT_TIME=`date +%Y%m%d%H%M'`
CURRENT_TIME_S=`date +%s`
STALE_TIME_S=`expr ${CURRENT_TIME_S} - ${SEC_BACK}`
echo "CURRENT_TIME=${CURRENT_TIME}"
echo "CURRENT_TIME_S=${CURRENT_TIME_S}"
echo "STALE_TIME_S=${STALE_TIME_S}"

CURRENT_TIME=201108091024
CURRENT_TIME_S=1312899850
STALE_TIME_S=1312899550

Now I just need to figure out how to get the STALE_TIME_S (seconds since January 1, 1970) into STALE_TIME (YYYYMMDDHHMM). I think I can use the date command or maybe TZ. Off the top of their head does anyone know?

---------- Post updated at 03:11 PM ---------- Previous update was at 10:31 AM ----------

Quote:
Originally Posted by Shell_Life
One possible solution is to place your script under cron to run every 5 minutes.

In the same script you can create a file with the current YYYYMMDDHHmm.

Every time you run your script, you read the only record in the timestamp file.

At the end, you overwrite it with the current date/time.
I went in a different direction.

Since I already have the OLDTIME in YYYYMMDDHHMM format I simply touch my temp file with it. Then do a find on my temp file and return the file name if its over 5 minutes old.
Code:
touch -t ${OLDTIME} ${TMP_FILE}
STALE_CHECK_05MIN=`find ${TMP_FILE} -mmin +5`

Now I can to a test if the STALE_CHECK_05MIN is null. If Its not null then I know its older than 5 minutes.

Last edited by Franklin52; 08-10-2011 at 10:35 AM.. Reason: Please use code tags for data and code samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

How to get a time minus 60 minutes?

Hello, date --date '-60 min ago' +'%Y-%m-%d %H:%M:%S,%3N' Above command gives the date and time minus 60 minutes but the problem i am facing is, i do not want to hardcode the value 60 it is stored in a variable var=60 now if i run below command , i get error date --date '-$var min... (3 Replies)
Discussion started by: Ramneekgupta91
3 Replies

3. Shell Programming and Scripting

Time difference in minutes

Hi Folks, I have a text file that has only time in the format HH:MM:SS like seen below. 21:36:17 23:52:08 I need to find the difference in minutes alone from this text file so the result would be 136. Thanks Jay (11 Replies)
Discussion started by: jayadanabalan
11 Replies

4. Shell Programming and Scripting

How to convert 24 Hr Time Format into Minutes?

Hello All I know the general Logic behind it but do not know the shell programming so much. For Example, The Time is stored in a given Variable if the Time is 0800 then i need to extract the last digits of the number and Add it to the Remaining Digit of the Number which is multiplied by... (7 Replies)
Discussion started by: Ajesh
7 Replies

5. Shell Programming and Scripting

To get the previous 5 minutes

Hi I need to get the previous 5 minutes from the currnet time as given below. Please help. Current time : date +%d-%m-%Y_%H-%M output : 16-05-2012_15-05 I need the previous 5 minutes of the above output , Required output: 16-05-2012_15-04 16-05-2012_15-03... (4 Replies)
Discussion started by: Kanchana
4 Replies

6. Shell Programming and Scripting

grep the time within given minutes

Mar 26 15:25:11 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 26 15:28:52 : jdoe : 3 incorrect password attempts ; TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 25 12:23:07 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; ... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

7. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

8. Shell Programming and Scripting

Process Time in hours or minutes only

Hi i want to print the time of a process in hours only..(or) in minutes only.Is there anyway to print the process such like that when i give the commnand like following #ps -eo pid,time PID TIME 412 01:49:32 481 00:03 it shows in HH:MM:SS format: Could anyone... (1 Reply)
Discussion started by: srikanthg
1 Replies

9. Shell Programming and Scripting

how to find the time before 30 minutes

Hi All, I want to find out the time before 30 minutes. I am able to do with in hours limit. date Fri Aug 21 06:50:00 BST 2009 TZ=CST+1 date Fri Aug 21 04:50:02 CST 2009 Can any one please help me (6 Replies)
Discussion started by: vikash_k
6 Replies

10. UNIX for Advanced & Expert Users

Adding # minutes to current time...

Hi all, Looking for a way to add lets say 10 minutes to the current time output should look like 7:15 AM or 7:15 PM. I know that gdate could do this for me but unfortunately its not available on the system I'm working on. So if any one know any way I can accomplish this using the date command it... (7 Replies)
Discussion started by: gptavares
7 Replies
Login or Register to Ask a Question