0

I am trying to make a copy of completed files that are saved to the torrent save folder. I have created a bash script using inotifywait to watch the folder. My original script worked great with files and folders/subfolders. Once I replaced the "-e create -e moved_to" with "-e close_write" it no longer recognizes folders that are saved in the watch folder. It only works now with files. The reason I am needing to change to this is because a lot of my files are large (and multiples in folders) and takes some time to be saved completely and the script runs through all the processes before they are done.

I have also tried the "wait" command but it does nothing at all. I know there are more complicated/better/correct ways of using it but I have not figured them out yet.

Here is a sample of my script. Any guidance would be appreciated. I am not well versed in writing bash scripts so sorry if it is a mess.

WATCHED=/mnt/Seeding
DESTINATION=/mnt/Complete-Torrents
user=userid
group=groupid
perms=777

# Step 1 - Copying completed torrents to Complete-Torrents folder leaving original torrents to seed 
inotifywait -m -e close_write --format %f $WATCHED \
    | while read new
        do
            echo Detected new torrent $new, Copying to Complete-Torrents folder
            cp -r "$WATCHED/$new" "$DESTINATION/"
            wait
                          
# Step 2 - Deleting unwanted files from Complete-Torrents folder

            echo Deleting unwanted files from Complete-Torrents folder
            find $DESTINATION -type f -iname "*.txt" -delete
            find $DESTINATION -type f -iname "*.nfo" -delete
            find $DESTINATION -type f -iname "*.website" -delete
            find $DESTINATION -type f -iname "*.exe" -delete
            find $DESTINATION -type f -iname "*.html" -delete
            find $DESTINATION -type f -iname "*.htm" -delete
            find $DESTINATION -type f -iname "*.sfv" -delete
            find $DESTINATION -type f -iname "*.parts" -delete
            find $DESTINATION -type f -iname "*.jpg" -delete
            find $DESTINATION -type f -iname "*.png" -delete
            find $DESTINATION -type f -iname "*.doc" -delete
            sleep 10
                           
# Step 3 - Change permissions of new files in Complete-Torrents folder

            echo Changing ownership and permissions to Complete-Torrents folder and files
            chown -R $user:$group "$DESTINATION"
            chmod -R $perms "$DESTINATION"
                           
        done

Thanks, Shawn

EDIT...

I cannot for the life of me get this script to even see a new folder added to the watch folder. It literally does NOTHING. If I replace the close_write with create & moved_to and make no other changes it sees the folder and contents and processes correctly. I find this very odd.

In fact I even tried to make a small test script to see if I can get it to work with a if/elif/else statement but once again when I copy a folder in the watch folder the script does nothing (doesn't even make to the loop). If I put a file in then it provides the correct output.

Here is the test script I ran. Can someone else confirm if they can get a new folder to be recognized and processed correctly?

#!/bin/bash
WATCHED=/mnt/Watched
inotifywait -re close_write --format '%w%f' -m $STEP1_WATCHED \
    | while read -r new
        do
            if [[ -d "$new" ]]; then            
            echo "Detected new folder $new"
            elif [[ -f "$new" ]]; then            
            echo "Detected new file $new"
            else
            echo "neither $new"
            fi
            done
    done
S. Ray
  • 1
  • 2
  • What is `$STEP1_WATCHED`? –  Dec 15 '20 at 21:09
  • Don't you have one 'done' too many in the test script? –  Dec 15 '20 at 21:14
  • I do not understand,. Did you read the manual? What does it say that `close_write` does? `Can someone else confirm if they can get a new folder to be recognized and processed correctly?` Why would you need that, it's clear after reading the man page, that `close_write` will not detect a new folder creation. It says `A watched file or a file within a watched directory was closed, after being opened in writeable mode` - why would you expect a directory creation to be detected? – KamilCuk Dec 15 '20 at 21:14
  • The test script works for me in my personal `~/tmp/` directory, which is not too deeply nested and/or populated. Setting up the watches can take some time! When the watches were set up, I `touch`ed a file in the directory and got `Detected new file /home//tmp/foo-bar-baz`. –  Dec 15 '20 at 21:15
  • 1
    KamilCuk...Wow. Good catch. Yes I have been over the manual many times and for some reason did not notice that it only applies to files. I guess if it said "close_write will not detect a new folder creation" specifically I would have caught it. But you are correct. Thanks for the catch! – S. Ray Dec 15 '20 at 21:21
  • 1
    Roadowl... Yes you are correct about the extra "done". KamilCuk pointed out that close_write does not see folders so my main issue is not an issue at this point. But I now need to find a different way of doing what I need. Thanks for the help. – S. Ray Dec 15 '20 at 21:23
  • You could `-e close_write -e create -e moved_to` and if you detect folder creation, then detect the changes also in that folder, recursively. But such design willl take some scripting. – KamilCuk Dec 15 '20 at 21:28
  • Hmm...Will have to think that one through. I guess I can close my 50 or so tabs and start fresh with this idea. LOL. Thanks KamilCuk! – S. Ray Dec 15 '20 at 21:32
  • Roadowl... Sorry for the errors in my test code. I had to copy and paste bits and pieces from other scripts to put it together quickly. Good catch! Thanks – S. Ray Dec 15 '20 at 21:33

1 Answers1

0

It turns out that the issue here was my mistake. In all my times reading through the inotify manual I somehow did not catch that this only works for files and not folders. I obviously was looking for something specific and glossed over what the manual was saying about close_write.

Thanks to all that helped!

S. Ray
  • 1
  • 2