calculating the time difference, when the script was executed and the currenent file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculating the time difference, when the script was executed and the currenent file
# 1  
Old 07-14-2008
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  
Old 07-14-2008
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  
Old 07-14-2008
Hammer & Screwdriver 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 12:20 PM.. Reason: added code to do math difference
# 4  
Old 07-14-2008
I made the script, but its giving the error

stat: not found.
# 5  
Old 07-14-2008
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  
Old 07-14-2008
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.
# 7  
Old 07-16-2008
Space character needed between the "f" and the "!".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating Time difference Between two Rows in Linux

16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

2. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

3. Shell Programming and Scripting

Trouble calculating difference in number of days

Hi all, I have a requirement to calculate the difference of number of days of time stamp of a file and system date and if the difference is greater than 15 days it should prompt as previous month file otherwise current month file. Below is the code i used and it is working fine till now. (You... (2 Replies)
Discussion started by: Ravindra Swan
2 Replies

4. Shell Programming and Scripting

Calculating script run time in Solaris OS

Hi All, I have written script and wanted to know the run time of it in seconds. i used below logic but am not getting the results in second instead getting error. cat pkloader.sh # if you want to calculate the time in milliseconds then use $(date +%s%N) START_TIME=`date +%s` echo... (2 Replies)
Discussion started by: Optimus81
2 Replies

5. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

6. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

7. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

8. Shell Programming and Scripting

Calculating the difference between dates

Hello! i need to find files lower and bigger that one date i pass, i search in the man find, but i didn't find anything, the only that i find is the parameter -mtime, in this parameter i can pass a number of days, but i need to know the difference between dates, any built-in function for do... (15 Replies)
Discussion started by: claw82
15 Replies

9. Shell Programming and Scripting

parsing and calculating difference.

Hi, I have a file with the contents as following Access Time: Thu Nov 6 16:43:45 2008 Modify Time: Thu Nov 6 16:43:45 2008 Change Time: Thu Nov 6 16:43:45 2008 Access Time: Thu Nov 6 16:43:02 2008 Modify Time: Thu Nov 6 16:44:01 2008 Change Time: Thu Nov 6 16:44:01 2008 I need... (3 Replies)
Discussion started by: meetmano143
3 Replies

10. Shell Programming and Scripting

Script to display time difference..

Hi i've written a script which reads last two line of the log file from N number of servers and send the mail by redirecting to a particular log file. And the two lines is displayed below. Oracle Q03 Begin Hot BACKUP Time: 07/23/08 18:35:46 Oracle Q03 End Hot BACKUP Time: 07/24/08 14:18:15... (1 Reply)
Discussion started by: suri.tyson
1 Replies
Login or Register to Ask a Question

Featured Tech Videos