The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To find the time difference between two lines of the same log file satyakam Shell Programming and Scripting 2 04-30-2008 11:46 AM
calculating sum of fields in a file dsravan Shell Programming and Scripting 4 01-03-2008 01:00 PM
Calculating field using AWK, or GAWK script Trellot UNIX for Dummies Questions & Answers 4 10-28-2007 03:43 PM
NTP - with time difference shauche AIX 0 07-02-2007 02:21 AM
time difference decci_7 UNIX for Advanced & Expert Users 3 10-09-2006 04:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-14-2008
velocitnitin velocitnitin is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
calculating the time difference, when the script was executed and the currenent file

Hi,

I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert.
I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the message.
  #2 (permalink)  
Old 07-14-2008
velocitnitin velocitnitin is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
calculating the time difference, when the script was executed and the current time

Hi,

I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert.

I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the message.
  #3 (permalink)  
Old 07-14-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Take a look at the stat command

stat -c %X ==>time of last access
stat -c %Y ==>time of last modification
stat -c %Z ==>time of last change

Thus, if you set a variable to capture the returned number, you can compare it to a number for now (i.e. touch a file and get similar value).

Code:
ch_file1=$(stat -c %Y file1)
touch file2
ch_file2=$(stat -c %Y file2)
then see if they are different by more than 300 [300 seconds = 5 minutes]

Code:
> file_diff=$((ch_file2-ch_file1))
> echo $file_diff
533

Last edited by joeyg; 07-14-2008 at 11:20 AM.. Reason: added code to do math difference
  #4 (permalink)  
Old 07-14-2008
velocitnitin velocitnitin is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
I made the script, but its giving the error

stat: not found.
  #5 (permalink)  
Old 07-14-2008
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
Hmm "stat" is not a HP UX command. It's on some Linux distributions but not a portable unix command.

Personally I'd use cron to run the script every 5 mins during the monitoring period.

Start the cron 5 minutes earlier than the start of your monitoring period.
Define a timestamp filename containing todays date:
YYYYMMDD="`date +%Y%m%d`"
my_timestamp="my_prefix${YYYMMDD}"

At the start of the script if the timestamp file does not exist, create it with unix "touch" and exit.

On the second and subsequent invocations the file will exist. You can then easily check whether the alert file timestamp is more than 5 minutes old with "find ... -type f ! -newer $my_timestamp ... ".

At the end of the script "touch" the timestamp file ready for next time.

You will need some code to delete old timestamp files, so choose a unique prefix for the timestamp filename.
Beware: Recursive alert log checkers can easily generate multiple alarms for one incident unless you code to deal with this.
  #6 (permalink)  
Old 07-14-2008
velocitnitin velocitnitin is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 6
Question

I cannot use the cron,as i am running the script through automation tool.
Also /tmp/nitin -type f -newer timestamp.sh is running fine but /tmp/nitin -type f! -newer timestamp.sh is giving error bad -type f!.

Is there any other way, to know that the particular file is older than 5 min.
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0