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
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!