Score:1

Need help with inotifywait loop only processing one file

qa flag

I have created a shell script that runs an inotifywait command.

It watches a folder for new image uploads (That comes from a website) and then uses Imagemagick to resize them, move them to another directory and then sends a command to a remote wordpress installation to import the images.

It looks like this (I've created it as a service, so it runs at startup)

#!/bin/bash

TARGET=/var/www/img/uploads
PROCESSED=/var/www/img/processed
DONE=/var/www/img/finished
URL=https://--censored--

inotifywait -m -r -e create -e moved_to --format "%f" $TARGET \
        | while read FILENAME
                do
                        echo Detected $FILENAME, moving and resizing
                        mv "$TARGET/$FILENAME" "$PROCESSED/$FILENAME"
                        convert -verbose -debug coder -log %e "$PROCESSED/$FILENAME" -resize 600x600 -background white -gravity center -extent 600x600 "$DONE/$FILENAME"
                        wp @mm media import "$URL/$FILENAME" --path=--censored--
                        echo Finished processing $FILENAME.
                done

The code is working, but if I upload multiple files it only uploads the first one. If I remove the Wordpress part it seems to process all files.

But I'm not sure where I'm going wrong.

uz flag
Jos
Have you confirmed that the `inotifywait` command generates a correct list of filenames, separated by newlines?
MadsK avatar
qa flag
In the log file it seems only one file is generated and processed. If I then wait and upload a new one it works. But if I upload two just after each other only one is processed. But I'm not sure how to check if each is created on new lines?
uz flag
Jos
Instead of piping it to the wp loop, write it to a temporary file to see what goes on, like this: `inotifywait -m -r -e create -e moved_to --format "%f" $TARGET > my.log.file`, then upload multiple images, then look at the output of the `inotifywait` process in `my.log.file`.
MadsK avatar
qa flag
It creates two separate lines - I just tried to simoultaniously add two files and the output in the log is file1.jpg file2.jpg I'm wondering if it is because the file transfer happens to fast, and it can't see all the changes? Can I add a pause or something like that?
uz flag
Jos
I believe you need to run `inotifywait` itself in a loop, as described here: https://linux.die.net/man/1/inotifywait, Example 2.
waltinator avatar
it flag
`inotifywait` waits for a Single event, and reports on it. Read `man -k inotify`.
MadsK avatar
qa flag
Ended up dividing the script into two separate processes so one for the convert and one for the upload. Setting a "sleep" before done in the process script seems to have been enough for the next event to happen. Thanks for the answers.
Jeff Schaller avatar
ru flag
@MadsK feel free to write an Answer (box below) with your solution!
Score:0
qa flag

Ended up dividing the script into two separate processes so one for the convert and one for the upload. Setting a "sleep" before done in the process script seems to have been enough for the next event to happen. Thanks for the answers.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.