Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a bash script upon file creation

Tags:

linux

bash

loops

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

like image 741
Simianspaceman Avatar asked Feb 04 '13 17:02

Simianspaceman


People also ask

Can you use to create a file using bash?

To create a new file, run the "cat" command and then use the redirection operator ">" followed by the name of the file. Now you will be prompted to insert data into this newly created file. Type a line and then press "Ctrl+D" to save the file. $ cat > secondFile.


1 Answers

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
like image 187
Satish Avatar answered Oct 24 '22 16:10

Satish