Zsolt Botykai
Zsolt Botykai

Reputation: 51603

Need to monitor directory change, and perform action

1st of all: I'm not programmer, neither Linux guru, just have to work with Linux, Oracle, shell scripts.

My current task is to monitor a table in Oracle (tool: sqlplus), and if it contains a certain row, then watch a linux directory for a growing tmp file, and log its attributes (e.g. ls -l), in every 5 second.

The most important part is: this tmp file will be deleted if the above record is deleted from the oracle table, and I need the last contents of this tmp file.

I can't control the Oracle data, just got query rights.

The available tools are: bash, awk, sed, some old version of perl, ruby (not 1.9*), and python (2.5). I don't have install rights, so most of the outside libraries are not accessible. I know I can run some libraries from my $HOME, but I don't have internet connection on that machine: so can't download any library.

Inotify is not available (older kernel).

Any idea where to start/how to do it? Thanks in advance.

Upvotes: 1

Views: 1849

Answers (2)

Sean Bright
Sean Bright

Reputation: 120644

This is ugly and naive... but...

#!/bin/bash

WASTHERE=0
MONITORING=/tmp/whatever.dat
LASTBACKUP=/tmp/mybackup.dat
LOGFILE=/tmp/mylog.log

# Just create an empty file to start with
touch "$LASTBACKUP"

while [ 1 ];
do
        if [[ ! -e "$MONITORING" ]]; then
                if [[ $WASTHERE -ne 0 ]]; then
                        echo "File is gone!  Do something with $LASTBACKUP";
                        WASTHERE=0
                fi
        else
                WASTHERE=1
                ls -l "$MONITORING" >> $LOGFILE
                cp "$MONITORING" "$LASTBACKUP"
        fi

        sleep 5
done

The unfortunate part about this is that if anything happens to the file being 'monitored' while the script is sleeping (content is written to it, for example) and the file is then deleted before the script wakes up, the newly written content will not be in the 'backup.'

Upvotes: 1

Sunny Milenov
Sunny Milenov

Reputation: 22310

How about creating a hard link in another directory, then, when the file "disappears" in the original location, the hard link will still have access to the content.

Upvotes: 4

Related Questions