Running a bash script while creating a file

I want to write a small bash script to, at startup, look at the directory for all newly created files. If a new file appears, I want its presence to run a second script to run.

I can see that this is used to start a recently digitized video compression and add it to the dropped frames log.

Currently my code is as follows:

#!/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 a simpler / cleaner / less memory intensive way to do this?

EDIT I will change the swallow directory to a capture session. I adjusted the code

+8
linux bash loops
source share
3 answers

What about incron ? It runs commands to modify files / directories.

 sudo apt-get install incron 

Example:

 <path> <mask> <command> 

Where <path> can be a directory (this means that the directory and / or files are viewed directly in this directory (not the files in subdirectories of this directory!)) Or the 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 a command that should be executed when an event occurs. The following substitutions can be used in the command specification:

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

If you are looking at a directory, then $ @ contains the directory path and the $ # file that raised the event. If you are watching a file, then $ @ contains the full 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-$# 

Check him

 $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. Add the following line to the menu.lst grub file:

 kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes 
+11
source share

Use iwatch . No, really. It will process all the details of creating a daemon, run at startup, monitoring and registration, and so on and so forth. All you have to do is set the parameters, and your bash script will handle the details of actually executing something with the file.

+1
source share

You can do this with the inotify magic tool:

 inotifywait -r -m ./YOUR_MONITORED_DIR | while read ab file; do [[ $b == *CREATE* ]] && ./another_script "$file" done 

This method has the great advantage of avoiding polling every N seconds.

Inotify (inode notify) is a subsystem of the Linux kernel that acts on file systems to notice changes in the file system and report changes to applications. It replaces the earlier tool, dnotify, which had similar goals.

http://en.wikipedia.org/wiki/Inotify
See inotify doc

0
source share

All Articles