The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
find command in while loop - how to get control when no files found? mavsman Shell Programming and Scripting 3 04-03-2008 11:44 AM
awk command to find the count of files ina directory sish78 Shell Programming and Scripting 11 07-19-2007 08:00 AM
how can i check in csh if command found or not found ? umen Shell Programming and Scripting 2 03-03-2007 07: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 09:17 AM
Restoring back files from "lost+found" directory dhasarath Filesystems, Disks and Memory 1 09-24-2002 09:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-13-2006
krkan krkan is offline
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.
  #2 (permalink)  
Old 03-13-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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
  #3 (permalink)  
Old 03-13-2006
botao's Avatar
botao botao is offline
Registered User
  
 

Join Date: Mar 2006
Location: brazil
Posts: 38
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
  #4 (permalink)  
Old 03-13-2006
krkan krkan is offline
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.
  #5 (permalink)  
Old 03-14-2006
izy100 izy100 is offline
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}
Sponsored Links
Closed Thread

Bookmarks

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 06:14 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