coppit Posted June 24, 2015 Share Posted June 24, 2015 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 Quote Link to comment
morbidpete Posted June 24, 2015 Share Posted June 24, 2015 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 Quote Link to comment
Squid Posted June 24, 2015 Share Posted June 24, 2015 Question: if im writing a file does it execute the script when the file starts writing or when its finished? Quote Link to comment
coppit Posted June 24, 2015 Author Share Posted June 24, 2015 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 Quote Link to comment
coppit Posted August 18, 2018 Author Share Posted August 18, 2018 (edited) 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, 2018 by coppit Mention polling feature. Quote Link to comment
Ryland Posted August 18, 2018 Share Posted August 18, 2018 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? Quote Link to comment
coppit Posted August 20, 2018 Author Share Posted August 20, 2018 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"? Quote Link to comment
Ryland Posted August 25, 2018 Share Posted August 25, 2018 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. Quote Link to comment
Ryland Posted August 25, 2018 Share Posted August 25, 2018 (edited) 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, 2018 by Ryland RTFM Quote Link to comment
alturismo Posted February 12, 2019 Share Posted February 12, 2019 hi, may a question if its possible to trigger a command wich will execute something in unraid then ? like a user script ... ? Quote Link to comment
Squid Posted February 12, 2019 Share Posted February 12, 2019 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 Quote Link to comment
alturismo Posted February 13, 2019 Share Posted February 13, 2019 yes, that probably describes it the best i ll look if i can find something wich fits ... thanks for the link. Quote Link to comment
Chad Kunsman Posted April 6, 2020 Share Posted April 6, 2020 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. Quote Link to comment
guayako Posted July 31, 2021 Share Posted July 31, 2021 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? Quote Link to comment
JoergHH Posted October 14, 2021 Share Posted October 14, 2021 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: Quote Link to comment
Squid Posted October 14, 2021 Share Posted October 14, 2021 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 Quote Link to comment
GonjaT Posted October 21, 2021 Share Posted October 21, 2021 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. Quote Link to comment
sloob Posted May 9, 2022 Share Posted May 9, 2022 (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 May 9, 2022 by sloob Quote Link to comment
sloob Posted May 17, 2022 Share Posted May 17, 2022 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? Quote Link to comment
mountainmantra Posted March 6 Share Posted March 6 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. Quote Link to comment
Recommended Posts
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.