Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

linux - Executing a bash script upon file creation

I am looking write a small bash script to, when launched, watch a directory for any newly created files. If a new file appears, I want its presence to trigger a second script to run.

I see this being used to trigger the compression recently digitized video, and add it to a log of ingested footage.

Currently my code looks like this:

#!/bin/sh

##VIDSTAT is a global variable coming from a parent script.
##proj is the ingestion directory coming from a parent script
proj=$1

dir="/home/$USER/data/movies/$proj"
dirlist=$(ls $dir)


while { $VIDSTAT -eq 1 }:
do
    for mov in $dirlist
    do
        if [ "$(( $(date +"%s") - $(stat -c "%Y" $mov) ))" -lt "5" ]
        then
        ~/bin/compressNlog.sh $mov
        fi
    done
done

Is there an easier/cleaner/less memory intensive way to do this?

EDIT I will be changing the ingestion directory per capture session. I have adjusted the code accordingly

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about incron? It triggering Commands On File/Directory Changes.

sudo apt-get install incron

Example:

<path> <mask> <command>

Where <path> can be a directory (meaning the directory and/or the files directly in that directory (not files in subdirectories of that directory!) are watched) or a file.

<mask> can be one of the following:

IN_ACCESS           File was accessed (read) (*)
IN_ATTRIB           Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
IN_CLOSE_WRITE      File opened for writing was closed (*)
IN_CLOSE_NOWRITE    File not opened for writing was closed (*)
IN_CREATE           File/directory created in watched directory (*)
IN_DELETE           File/directory deleted from watched directory (*)
IN_DELETE_SELF           Watched file/directory was itself deleted
IN_MODIFY           File was modified (*)
IN_MOVE_SELF        Watched file/directory was itself moved
IN_MOVED_FROM       File moved out of watched directory (*)
IN_MOVED_TO         File moved into watched directory (*)
IN_OPEN             File was opened (*)

<command> is the command that should be run when the event occurs. The following wildards may be used inside the command specification:

$$   dollar sign
$@   watched filesystem path (see above)
$#   event-related file name
$%   event flags (textually)
$&   event flags (numerically)

If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty.

Working Example:

$sudo echo spatel > /etc/incron.allow
$sudo echo root > /etc/incron.allow

Start Daemon:

$sudo /etc/init.d/incrond start

Edit incrontab file

$incrontab -e
/home/spatel IN_CLOSE_WRITE touch /tmp/incrontest-$#

Test it

$touch /home/spatel/alpha

Result:

$ls -l /tmp/*alpha*
-rw-r--r-- 1 spatel spatel 0 Feb  4 12:32 /tmp/incrontest-alpha

Notes: In Ubuntu you need to activate inotify at boot time. Please add following line in Grub menu.lst file:

kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...