"Run a command if the folder changes" container

Recommended Posts

Edit Jun26: Updated for the new USER_ID, GROUP_ID, and UMASK config settings.


Hi all,


I've created a container that uses inotify to watch a directory for changes, then invoke a user-specified command.


Documentation is here: https://registry.hub.docker.com/u/coppit/inotify-command/


The easiest way to use it is to paste this URL into your template repositories, then use the template when creating a new container: https://github.com/coppit/docker-templates


You create a config file for each dir/command pair. There are lots of ways to use this. Here's an example:


newperms.conf, for running newperms whenever there's a change in the cache directory:


COMMAND="/root/newperms /dir2"
# This is important because chmod/chown will change files in the monitored directory


sagetv.conf, for telling SageTV to rescan its imported media when the media directory changes:


COMMAND="wget -nv -O /dev/null --auth-no-challenge http://sage:frey@"
# This is not important because the above is a "fire and forget" asynchronous operation


Then I create a container that maps the proper directories for /dir1 and /dir2, and also the newperms utility:


docker run --name=inotify-command -d -v /etc/localtime:/etc/localtime \
  -v /mnt/vms/docker-config/inotify-command:/config:rw \
  -v /mnt/user/Media:/dir1 -v /mnt/cache:/dir2:rw \
  -v /usr/local/sbin/newperms:/root/newperms coppit/inotify-command

Link to comment

Question:  if im writing a file does it execute the script when the file starts writing or when its finished?


Excellent question. It triggers when the file is closed in write mode, not when it's created, read, or written. inotify is a bit funky. For example, it also triggers for new directories, for the case where someone copies a directory containing a ton of files into the watch dir.


I thought about allowing the user to customize the events, but given how funky inotify is, I figured that would be too complicated for most users.


Note that this doesn't prevent your command from stomping all over a file that's being written, after being triggered by another file that was write-closed. So, for example, you wouldn't want a command that moves files out of the watch dir blindly -- it would need to use lsof or something to determine which are safe to move.


Here's the full list of file events:








And dir events:







Link to comment
  • 3 years later...

Moved to Alpine linux, reducing the image size from 232MB to 90MB



  • Make the change monitors more like services that restart if they crash.
  • Added a feature to use polling, so that Windows shares can be monitored too.
Edited by coppit
Mention polling feature.
Link to comment

I added the inotify-command docker and set the host path to point at the directory I want to monitor.  The docker never starts and nothing is showing up in the sytem log to explain why.  I didnt modify either Key 1 or Key 2.


Edit: And Im an idiot, RTFM.  I thought I had to get it up and running so I could access its console to modify sample.conf, nope.  I have it up and running but now need to install comskip.  Any chance of cloning this into a repo that automatically installs comskip?

Edited by Ryland
Link to comment
  • 5 months later...
  • 1 year later...

It would be great if we could tweak what functions we would want to use (or not use). Maybe via container variables?


For example, I have a 'dropbox' style file upload app that I use that doesn't require authentication to upload files and deletes them automatically when retention timer is up. It's dead simple and I like it that way. But I want to watch for files getting created(uploaded) in that directory and not at all if those files are deleted. 


So would be great to be able to exclude DELETE,ISDIR for example

Link to comment
  • 1 year later...
  • 2 months later...
  • 6 months later...
Posted (edited)

Bit of a noob here, Would it be possible to get the path of where the detected file change occured and use it in the command?


Example: docker exec -i nextcloud sudo -u abc php /config/www/nextcloud/occ files:scan admin --path="/admin/<INOTIFY PATH HERE>"

Edited by sloob
Link to comment
On 5/9/2022 at 1:11 PM, sloob said:

Bit of a noob here, Would it be possible to get the path of where the detected file change occured and use it in the command?


Example: docker exec -i nextcloud sudo -u abc php /config/www/nextcloud/occ files:scan admin --path="/admin/<INOTIFY PATH HERE>"

I'm guessing no-one has experience with this?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.