0

The script below watches for close_write events in the TARGET directory and does something with the file

#!/bin/bash

TARGET=/home/*/ftp/files

inotifywait -m -r -e close_write $TARGET |
        while read directory action file
        do
                if [[ "$file" =~ .*jpg$ ]] || [[ "$file" =~ .*mp4$ ]]
                then
                        # do sth with the file
                fi
        done

So right now it is correctly watching for close_write events for files under e.g. /home/sammy/ftp/files and /home/neo/ftp/files.

The problem I'm facing is once a new user created, let's say mike, then the watch seems to no longer be working and I will have to re-execute the script so that it will know about the new user's path, i.e. the nohup process needs to be killed and be ran again.

Is there any way for inotifywait to know about newly created directories ?

0

1 Answer 1

2

With -r it automatically finds new directories created under the ones it's watching. But here, it gets the paths /home/sammy/ftp/files and /home/neo/ftp/files (etc.), and /home/mike isn't under either of them, so it won't be detected. (Remember, it's the shell that expands the glob /home/*/ftp/files before inotifywait even runs.)

In principle, you could watch the whole of /home and manually ignore anything that wasn't in /home/*/ftp/files. But that would create piles of watches and produce loads of unwanted events.

A better idea might be to watch /home in addition to the /home/*/ftp/files directories, and to restart the watch when seeing a new subdirectory added under /home. If any new users always get the ftp/files directory created immediately, that shouldn't be too bad. You should probably explicitly wait for the new ftp/files directory to appear though, so that you don't fall into the race where your script resets the inotifywait before the new ftp/files is created, thus missing it.

(Though restarting inotifywait creates a window where a new file could be created, with the script missing it. Stuff like this would probably be safer to do with some programming language that supports inotify directly.)

A completely different approach would be to change the directory structure so that instead of /home/$user/files/ftp, the related stuff would be in /srv/ftp/$user. Then you could just add a watch on /srv/ftp, automatically catching new users without problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .