Monitoring a directory for new files with .xx and executing command if found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Monitoring a directory for new files with .xx and executing command if found
# 1  
Old 03-13-2006
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  
Old 03-13-2006
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  
Old 03-13-2006
Tools 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  
Old 03-13-2006
Power

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  
Old 03-14-2006
#!/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}
This User Gave Thanks to izy100 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if files inside a text file are found under a directory

Hi all, Please somebody help me with this: I want to check if the files listed in a text file, are found under a directory or not. For example: the file is list_of_files.txt, which contains inside this rows: # cat list_of_files logs errors paths debug # I want to check if these... (3 Replies)
Discussion started by: arrals_vl
3 Replies

2. UNIX for Dummies Questions & Answers

Realtime monitoring of files in a directory

Hello Experts, Here are my requirements. I want to monitor a directory and retrieve file properties like file names along with file sizes of files being generated today and that exceeds a threshold limit (say 10MB) using a shell script. Basically this shell script should monitor (kinda... (1 Reply)
Discussion started by: RamanM
1 Replies

3. Shell Programming and Scripting

Executing a command line in a directory and its subdirectories

I'm finally about to make the jump from PHP's mysql driver to the newer mysqli one using the mysqli converter tool. The tool is called by this command line: php /usr/share/nginx/html/rather/converter/cli.php -d -p ".php,.shtml,.inc" -u -b -v -w >> /tmp/convert.log e.g. at the web root:... (2 Replies)
Discussion started by: dheian
2 Replies

4. Shell Programming and Scripting

log monitoring continously and executing commands

Hi I want a script which continously searches for a work "MAX xonnections reached" in log file . As soon as it sees the message it needs to do series of steps 1) execute netstat command and redirect output to a file in /tmp 2) again continue monitoring the log file . this job should run until... (2 Replies)
Discussion started by: mskalyani9
2 Replies

5. Shell Programming and Scripting

getting the error 'not found' while executing the script

Hi, I am not able to figure out what the problem is: getting the following error sqltst.sh: 1: not found here is the script #!/bin/sh . /home/dev1/.profile . /home/dev1/.infenv `sqlplus -s $REPDB_LOGON << EOF SET SERVEROUT ON SET FEEDBACK OFF SET HEADING OFF SET TRIMSPOOL... (4 Replies)
Discussion started by: svajhala
4 Replies

6. Shell Programming and Scripting

How to strip ^M at end of each files for all files found in current directory

I am trying to use a loop to strip of the funny character ^M at the end of all lines in each file found in current directory and I have used the following in a script: find . -type f -name '*.txt' | while read file do echo "stripping ^M from ..." ex - "$file" > $tempfile %s/^M//g wq! # mv... (4 Replies)
Discussion started by: bisip99
4 Replies

7. Shell Programming and Scripting

CD (change directory) - Not advisable for executing a command

Hi, This is a quick one. I have got a review comment in one of the scripts that i wrote: "In UNIX script it not advisable to use cd to a directory and then run command." Is this true? I was trying to cd to log directory and use some cat or head or ls command. Many Thanks, Sam (2 Replies)
Discussion started by: samplify
2 Replies

8. Shell Programming and Scripting

print as well as count the files found by find command

I want the output of the find command to be printed and also the total files found by it. Can someone help in this. Obviously $ find . -type f | wc -l will not output the files found but only the count. I want both. There can be millions and trillions of files so dont want the output of find... (3 Replies)
Discussion started by: amicon007
3 Replies

9. Shell Programming and Scripting

find command in while loop - how to get control when no files found?

I have the following statement in script: find ${LANDING_FILE_DIR}${BTIME_FILENAME_PATTERN2} -print | while read file; do ... done When there are no files located by the find comand it returns: "find: bad status-- /home/rnitcher/test/....." to the command line How do I get control in... (3 Replies)
Discussion started by: mavsman
3 Replies

10. Filesystems, Disks and Memory

Restoring back files from "lost+found" directory

Hi Friends, How can I Restore the Files present under "lost+found" Directory of a FileSystem (in Solaris & Tru64 OS) to their original Locations. Now-a-days I am loosing lots of files in 2 of my Machines, One running Solaris8 and other Tru64(Digital) Unix. Thanx in... (1 Reply)
Discussion started by: dhasarath
1 Replies
Login or Register to Ask a Question