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
Date Calculations mitschcg UNIX for Dummies Questions & Answers 8 03-06-2009 04:40 AM
Time Calculations & Conversions Nysif Steve UNIX for Advanced & Expert Users 1 09-14-2007 12:07 PM
Time Calculations Nysif Steve UNIX for Dummies Questions & Answers 5 09-14-2007 09:49 AM
Time difference calculations satnamx Shell Programming and Scripting 1 07-26-2006 08:44 AM
Float calculations sharmavr UNIX for Dummies Questions & Answers 1 07-26-2006 03:18 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 05-16-2007
jonathan184 jonathan184 is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
problem doing the time calculations in shell.

I am trying to do a shell script to monitor if any files went through in the last hour.

There is a script in cron that runs every sec checking to see if a file is there and ftp the file out of this folder.

Now I just want to add a block of code that will check to see no files went in the last hour send an email.

ftp----------> folder to monitor ----------->ftp

This is what i came up with so far. I am getting a little problem calculating the time. If someone could help me out here please.



Code:


Code:
#Set the path to the folder to monitor
SUBJECT="DWH FILES WERE NOT SENT IN THE LAST HOUR"
FILE1="/home/tibco/javastuff/test/dwh*"


cd /home/tibco/javastuff/test/

###### Run list command
ls -l

time=`echo $line | awk '{ split($8,a,":"); print (a[1]*60)+a[2] }'`
FILEMATCH=`echo $line | awk '{ print $9 }'`


if [ $FILE1 = $FILEMATCH ]
  then
     echo "$time" > dwhtime.txt

     else
######## Run date command ##########
          date
          time2=`echo $line | awk '{ split($4,a,":"); print (a[1]*60)+a[2] }'`
       if [ $time2 -gt $time ]
           then
           mail -s "$SUBJECT" testemail <<EOF
          $SUBJECT
          $time
EOF
        fi

fi

  #2 (permalink)  
Old 05-16-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Jonathan,
One way you can do this is:

Code:
for newFile in `find . -type f -newer special_named_file`
do
  echo "File = "$newFile
done
touch special_named_file

  #3 (permalink)  
Old 05-16-2007
jonathan184 jonathan184 is offline
Registered User
  
 

Join Date: May 2007
Posts: 8
thats a good idea i will do that. Abou tthe time calcuations, do you have any idea how i should do this

In the code i am taking the time used in the ls -l command for that file and I am writing the time to a file, so when another file comes through it will overwrite the old time. So my issue is when no files comes for an hour I want it send an email

So would it be $time2 - $time1 = $b

if $b is greater than 60 mins then send email

How would i do this part?
  #4 (permalink)  
Old 05-16-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
You missed the whole point of Shell_Life's comment. The idea is to ask "find" for a list of files that are newer than 60 minutes. If that list is empty, you send mail. Touching the file after the loop finishes would work if your script is run every 60 minutes. If that is not the case, you could construct a timestamp 60 minutes in the past, and touch the file with the -t option at the start of the script. But to answer your question, the easiest way to check if a file is 60 minutes old or not would be to use perl. Here are a couple of perl one liners you can use in your script:
Code:
$ perl -e '@d=localtime ((stat(shift))[9]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' file1.txt
20070204053859
$ perl -e '@d=localtime time()-3600; printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'
20070516121610
$

The first outputs a timestamp for the file mentioned as the last argument. The second output a timestamp 60 minutes in the past.
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

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:48 AM.


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