June 24, 201511 yr 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:[email protected]: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
June 24, 201511 yr 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
June 24, 201511 yr Question: if im writing a file does it execute the script when the file starts writing or when its finished?
June 24, 201511 yr Author 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
August 18, 20187 yr Author 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 August 18, 20187 yr by coppit Mention polling feature.
August 18, 20187 yr 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?
August 20, 20187 yr Author 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"?
August 25, 20187 yr 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.
August 25, 20187 yr 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 September 5, 20187 yr by Ryland RTFM
February 12, 20197 yr hi, may a question if its possible to trigger a command wich will execute something in unraid then ? like a user script ... ?
February 12, 20197 yr 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
February 13, 20197 yr yes, that probably describes it the best i ll look if i can find something wich fits ... thanks for the link.
April 6, 20206 yr 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.
July 31, 20214 yr I have seem that inotify is use with Autoscan to send a scan request to plex, yet I can;t figure out the syntax in inotify config line for "command" to connect to Autoscan and send scan request to plex. Can I get some help if anyone got it done?
October 14, 20214 yr Does anyone know how I can call/use the Unraid server's notify from within the inotify-command container? Unfortunately, this solution no longer works:
October 14, 20214 yr You would also need to include another entry on the curl (-d csrf_token={$csrftoken} which you can get from /var/local/emhttp/var.ini But it changes with every reboot But if you read further down, the script was there
October 21, 20214 yr setuser: cannot execute /root/newperms: [Errno 2] No such file or directory any Idea why I get this error? I have Newperms under settings on unraid. Thanks.
May 9, 20224 yr 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 May 9, 20224 yr by sloob
May 17, 20224 yr 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?
March 6, 20242 yr 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 looking for an answer to this exact question. I'm writing a script that i want to trigger on file creation to create hardlinks/manage my photo collection, I can write around this, but having a simple script run to create hardlinks for each image would be a lot easier than building a script to monitor and have conditions for when it can run.
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.