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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find command in while loop - how to get control when no files found? mavsman Shell Programming and Scripting 3 04-03-2008 08:44 AM
awk command to find the count of files ina directory sish78 Shell Programming and Scripting 11 07-19-2007 05:00 AM
how can i check in csh if command found or not found ? umen Shell Programming and Scripting 2 03-03-2007 04:38 PM
can we list other than c files in a directory with only 'ls' command? venkat UNIX for Dummies Questions & Answers 3 03-12-2004 06:17 AM
Restoring back files from "lost+found" directory dhasarath Filesystems, Disks and Memory 1 09-24-2002 06:40 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-13-2006
Registered User
 

Join Date: Mar 2006
Posts: 2
Question Monitoring a directory for new files with .xx and executing command if found

Hi Guys.

I am a complete shell scripting newbie with some syntax and commands understanding. I'm more of a win admin. With that said: I need to write a shell script to monitor a directory '/Mon_Dir' for new occurrences of files with .xx extension. Once a new file is detected in the directory, a command 'str_xx -i file.xx' must be executed against that file. This script should be running as a service with 'sleep' statement or as a Cron job.

My understanding is that this shouldn't be much of a challenge for someone with scripting knowledge, which I don't have.

Any help will be greatly appreciated.
Reply With Quote
Forum Sponsor
  #2  
Old 03-13-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
Based on what you gave, here is a start, sleeping for 30 seconds, looping forever:
Code:
#!/bin/ksh
cd /Mon_dir
while true
do
    for file in `ls *.xx`
    do
          /path/to/str_xx -i $file
    done
    sleep 30
done
Reply With Quote
  #3  
Old 03-13-2006
botao's Avatar
Registered User
 

Join Date: Mar 2006
Location: brazil
Posts: 10
Cool scan & process & delay

--> process, hide, delay, all over again ...

Quote:
# place to put "processed" files
[ -d prok.d ] || mkdir prok.d
# loop
while true
do
# search for *.xx
for this in *.xx
do
# process
str_xx -i $this
# hide it (to avoid re-processing)
mv $this prok.d
done
# rest for 1 minute
sleep 60
done
Reply With Quote
  #4  
Old 03-13-2006
Registered User
 

Join Date: Mar 2006
Posts: 2
Red face

Thanks guys. This helps.
I used Jim's post and it works fine.
but looking at botao's post I realize that this might be something that Is more viable.

Scenario: After the file is found in the directory and the command is ran against the file. How will I then avoid running the same command against the same file again. Could the name of the file that the command ran agains be moved into a text file which the script will check before running the command again?

This would be most helpfull as I would like to avoid looping the command against the same files along with the new ones added to the directory.


BTW: you guys are awsome. Thanks a bunch.
Reply With Quote
  #5  
Old 03-14-2006
Registered User
 

Join Date: Nov 2002
Location: Singapore
Posts: 128
#!/bin/ksh
#
# UNIX Monitor New File Script
# Purpose: Monitors the new files in a directory
# Notifies via e-mail
# Outputs: Email
# Author : Steven Koh
#*********************************************************
#

if test $# -lt 1; then
echo "Usage: $0 <DIR TO MONITOR FOR NEW FILES> <FILENAME to exclude, delimited by |>"
echo "Example: $0 /var/tmp \"Test1|Test2\""
echo

exit 0;
fi

MONITOR_DIR=$1
IGNORE_LIST=$2

OS=`uname -s`

CONF_FILE=/usr/local/scripts/monitor.conf
SCRIPTDIR=`grep "SCRIPTDIR" $CONF_FILE | awk -F= '{print $NF}'`

LOCK=/tmp/${LOGNAME}.mon_new_file.lock
if test -f $LOCK; then
echo "${LOCK} exists. Exiting this job now ... " | mailx -s "$0" ${LOGNAME}
exit
fi

touch ${LOCK}

# The directory this script resides in
ADMINDIR=$SCRIPTDIR/monitorNEWFiles
#IGNORE_LIST=$ADMINDIR/ignore.List

MAILADD=${LOGNAME}@localhost

# Define the hostname of the server
SRVNM=`uname -n`

# Define the hostname of the SNMP server
SNMP_SRV=`grep "SNMP_SRV" $CONF_FILE | awk -F= '{print $NF}'`
SEND_TRAP=`grep "SEND_TRAP" $CONF_FILE | awk -F= '{print $NF}'`


echo ${MONITOR_DIR}
DIR_ID=`echo ${MONITOR_DIR} | sed 's/\//_/'g`
NEW_LOG="/tmp/${SRVNM}_${LOGNAME}_${DIR_ID}_mon_new_file.log"
OLD_LOG="/tmp/${SRVNM}_${LOGNAME}_${DIR_ID}_mon_new_file.old"
DIFF_FILE="/tmp/${SRVNM}_${LOGNAME}_${DIR_ID}_mon_new_file.same"

touch ${NEW_LOG}
mv ${NEW_LOG} ${OLD_LOG}

echo "============= `date` =============" | tee $NEW_LOG
unalias ls
ls -tr ${MONITOR_DIR} | tee -a ${NEW_LOG}


if test $# -eq 1; then
egrep -vf ${OLD_LOG} ${NEW_LOG} | grep -v "=============" > ${DIFF_FILE} 2>&1
else
egrep -vf ${OLD_LOG} ${NEW_LOG} | egrep -v "(${IGNORE_LIST})" | grep -v "=============" > ${DIFF_FILE} 2>&1
fi

if test `wc -l < ${DIFF_FILE}` -gt 0; then
echo "UNIX Monitor New Files - Monitor new files in ${MONITOR_DIR}"
echo "New Files for past 2 runs"
echo "=================================="
echo
cat ${DIFF_FILE}
echo

################
### Sending Mail
mail ${MAILADD} <<EOF
From: $0
To: $MAILADD
Subject: UNIX Monitor New Files - Monitor new files in ${MONITOR_DIR}

New Files for past 2 runs
==================================

`cat ${DIFF_FILE}`

EOF

fi

rm ${LOCK}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0