Run a script when a file is modified/time stamp changed | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Run a script when a file is modified/time stamp changed

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-21-2011
aemunathan aemunathan is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 April 2012, 8:07 PM EDT
Posts: 113
Thanks: 3
Thanked 0 Times in 0 Posts
Run a script when a file is modified/time stamp changed

Hi,

I need to run a script file which uses a file and that file is modified as and when some alarms generated, it is not based on any fixed time period.. it may be modified even once in a minute for some time and once in 30 min or once in 20 min. Hence i need to watch for the timestamp change of this file and run some scripts to process that. Since the file is overwritten i have to follow this kind of process.

pls guide me..

---------- Post updated at 05:44 PM ---------- Previous update was at 05:35 PM ----------

Hope this may help

Code:
time=$(ls -l $1 | awk -F" "  '{ print $7 }')
while :
do
   chk_time=$(ls -l $1 | awk -F" "  '{ print $7 }')
   if [ "$chk_time" != "$time" ]
   then
      echo "File modified"
      mailx -s "File $1 modified"  abc.dfg@xxx.com
      break; # if you want to stop the script once mail is send
   fi
   sleep 5  # 5sec wait time
done

Sponsored Links
    #2  
Old 04-21-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
so what is it that you want?

regards,
Ahamed
Sponsored Links
    #3  
Old 04-23-2011
aemunathan aemunathan is offline
Registered User
 
Join Date: May 2008
Last Activity: 17 April 2012, 8:07 PM EDT
Posts: 113
Thanks: 3
Thanked 0 Times in 0 Posts
Hi,

In a hurry i posted that requirement and searched immediately and got some clue...but i tried the following way i could not succeed.

Code:
#!/bin/bash
time=$(ls -l /home/dg/sm.ini | /usr/xpg4/bin/awk -F" "  '{ print $8 }')
timehr=$(echo $time | /usr/xpg4/bin/awk -F: '{print $1}')
timemin=$(echo $time | /usr/xpg4/bin/awk -F: '{print $2}')
while :
do
   chk_time=$(ls -l /home/dg/sm.ini | /usr/xpg4/bin/awk -F" "  '{ print $8 }')
   chk_timehr=$(echo $chk_time | /usr/xpg4/bin/awk -F: '{print $1}')
   chk_timemin=$(echo $chk_time | /usr/xpg4/bin/awk -F: '{print $2}')
if [ "$chk_timehr" != "$timehr" ] && [ "$chk_timemin" != "$timemin" ]
   then
     echo "/home/dg/SM/SMfile"
      sleep 5
      echo "/home/dg/SM/SMomAlarm"
   fi
   sleep 5  # 5sec wait time
done

pls help me
    #4  
Old 04-23-2011
ahamed101's Avatar
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 15 May 2014, 2:54 PM EDT
Location: San Jose, CA
Posts: 1,906
Thanks: 54
Thanked 485 Times in 478 Posts
Try this


Code:
#!/bin/bash
while :
do
   chk_time=$(ls -l run | awk -F" "  '{ print $8 }')
   chk_timehr=$(echo $chk_time | awk -F: '{print $1}')
   chk_timemin=$(echo $chk_time | awk -F: '{print $2}')
   if [ "$chk_timehr" != "$timehr" ] || [ "$chk_timemin" != "$timemin" ]
   then
     time=$(ls -l run | awk -F" "  '{ print $8 }')
     timehr=$(echo $time | awk -F: '{print $1}')
     timemin=$(echo $time | awk -F: '{print $2}')
     echo "/home/dg/SM/SMfile"
     sleep 5
     echo "/home/dg/SM/SMomAlarm"
   fi
   sleep 5  # 5sec wait time
done

regards,
Ahamed
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Is there any way to set the files modified date and stamp to last modifies time? saluja.deepak Emergency UNIX and Linux Support 7 08-08-2011 09:58 AM
Changing File Time Stamp (Bash Script) chj Shell Programming and Scripting 7 03-05-2010 04:44 PM
append date time stamp via ftp script sknisely Shell Programming and Scripting 1 12-31-2009 12:06 PM
how do i put a time stamp in a file name jhamm Shell Programming and Scripting 5 01-29-2007 09:00 AM
File date and time stamp Xenon UNIX for Dummies Questions & Answers 1 10-09-2001 03:58 PM



All times are GMT -4. The time now is 11:50 AM.