Sponsored Content
Top Forums Shell Programming and Scripting Monitoring a directory for new files with .xx and executing command if found Post 101988 by izy100 on Tuesday 14th of March 2006 10:16:20 PM
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:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
SHLOCK(1)						      General Commands Manual							 SHLOCK(1)

NAME
shlock - create lock files for use in shell scripts SYNOPSIS
shlock -p pid -f name [ -b ] [ -u ] [ -c ] DESCRIPTION
Shlock tries to create a lock file named name and write the process ID pid into it. If the file already exists, shlock will read the process ID from the file and test to see if the process is currently running. If the process exists, then the file will not be created. Shlock exits with a zero status if it was able to create the lock file, or non-zero if the file refers to currently-active process. OPTIONS
-b Process IDs are normally read and written in ASCII. If the ``-b'' flag is used, then they will be written as a binary int. For compatibility with other systems, the ``-u'' flag is accepted as a synonym for ``-b'' since binary locks are used by many UUCP pack- ages. -c If the ``-c'' flag is used, then shlock will not create a lock file, but will instead use the file to see if the lock is held by another program. If the lock is valid, the program will exit with a non-zero status; if the lock is not valid (i.e., invoking shlock without the flag would have succeeded), then the program will exit with a zero status. EXAMPLES
The following example shows how shlock would be used within a shell script: LOCK=/var/run/innd/LOCK.send trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15 if shlock -p $$ -f ${LOCK} ; then # Do appropriate work else echo Locked by `cat ${LOCK}` fi HISTORY
Written by Rich $alz <rsalz@uunet.uu.net> after a description of HDB UUCP locking given by Peter Honeyman. This is revision 1.9, dated 1996/10/29. SHLOCK(1)
All times are GMT -4. The time now is 06:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy