Jump to content
coppit

"Run a command if the folder changes" container

12 posts in this topic Last Reply

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:

 

WATCH_DIR=/dir2
SETTLE_DURATION=5
MAX_WAIT_TIME=30
MIN_PERIOD=30
COMMAND="/root/newperms /dir2"
USER_ID=0
GROUP_ID=0
UMASK=0000
# This is important because chmod/chown will change files in the monitored directory
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1

 

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

 

WATCH_DIR=/dir1
SETTLE_DURATION=5
MAX_WAIT_TIME=05:00
MIN_PERIOD=10:00
COMMAND="wget -nv -O /dev/null --auth-no-challenge http://sage:frey@192.168.1.102:8080/sagex/api?c=RunLibraryImportScan&1="
USER_ID=0
GROUP_ID=0
UMASK=0000
# This is not important because the above is a "fire and forget" asynchronous operation
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=0

 

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

Share this post


Link to post

Cool docker. Ill keep this in mind. Would have been great to update plex using wget. I now use cURL and a bat file called by my download and parser. I'm sure ill find a use for it. Thnaks

Share this post


Link to post

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

Share this post


Link to post

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:

 

ATTRIB

CLOSE_WRITE,CLOSE

MOVED_TO

MOVED_FROM

DELETE

 

And dir events:

 

ATTRIB,ISDIR

CREATE,ISDIR

MOVED_TO,IS_DIR

MOVED_FROM,IS_DIR

DELETE,ISDIR

Share this post


Link to post

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

 

Also:

  • 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.

Share this post


Link to post

Do you have a way to add in something like comskip to your docker such that I could have the docker monitoring for files appearing in a directory and automatically start comskip?

Share this post


Link to post

How about if I added a bash script to the config dir, and you could put any command you want in there, including "yum install comskip"?

Share this post


Link to post

I guess if I have access to the command line I could install comskip myself then set it up to monitor a directory and run a script if an item is detected.

Share this post


Link to post

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
RTFM

Share this post


Link to post

hi, may a question if its possible to trigger a command wich will execute something in unraid then ?

 

like a user script ... ?

Share this post


Link to post
1 hour ago, alturismo said:

hi, may a question if its possible to trigger a command wich will execute something in unraid then ?

 

like a user script ... ?

You want to leverage the inotifywait command

https://linux.die.net/man/1/inotifywait

Share this post


Link to post

yes, that probably describes it the best ;) i ll look if i can find something wich fits ... thanks for the link.

Share this post


Link to post

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.

Guest
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.