![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
||||
|
||||
|
--> process, hide, delay, all over again ...
Quote:
|
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
#!/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} |
|||
| Google The UNIX and Linux Forums |