Monitor folder for new files using unix ksh shell

2019-03-26 05:59发布

I've been Googling and Overflowing for a bit and couldn't find anything usable.

I need a script that monitors a public folder and triggers on new file creation and then moves the files to a private location.

I have a samba shared folder /exam/ple/ on unix mapped to X:\ on windows. On certain actions, txt files are written to the share. I want to kidnap any txt file that appears in the folder and place it into a private folder /pri/vate on unix. After that file is moved, I want to trigger a separate perl script.

EDIT Still waiting to see a shell script if anyone has any ideas... something that will monitor for new files and then run something like:

#!/bin/ksh
mv -f /exam/ple/*.txt /pri/vate

8条回答
成全新的幸福
2楼-- · 2019-03-26 06:39

I'm late to the party, I know, but in the interests of completeness and providing info to future visitors;

#!/bin/ksh
# Check a File path for any new files
# And execute another script if any are found

POLLPATH="/path/to/files"
FILENAME="*.txt" # Or can be a proper filename without wildcards
ACTION="executeScript.sh argument1 argument2"
LOCKFILE=`basename $0`.lock

# Make sure we're not running multiple instances of this script
if [ -e /tmp/$LOCKFILE ] ; then 
     exit 0
else
     touch /tmp/$LOCKFILE
fi

# check the dir for the presence of our file
# if it's there, do something, if not exit

if [ -e $POLLPATH/$FILENAME ] ; then
     exec $ACTION
else
     rm /tmp/$LOCKFILE
     exit 0
fi

Run it from cron;

*/1 7-22/1 * * * /path/to/poll-script.sh >/dev/null 2>&1

You'd want to use the lockfile in your subsequent script ( $ACTION ), and then clean it up on exit, just so you don't have any stacking processes.

查看更多
做自己的国王
3楼-- · 2019-03-26 06:39
#!/bin/ksh
while true
do
    for file in `ls /exam/ple/*.txt`
    do
          # mv -f /exam/ple/*.txt /pri/vate
          # changed to
          mv -f  $file  /pri/vate

    done
    sleep 30
done
查看更多
登录 后发表回答