Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

"Run a command if the folder changes" container

Featured Replies

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

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

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

  • 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

  • 3 years later...
  • 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 by coppit
Mention polling feature.

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?

  • 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"?

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.

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

  • 5 months later...

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

 

like a user script ... ?

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

  • 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

  • 1 year later...

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?

  • 2 months later...

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:

 

 

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

 

 

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.

  • 6 months later...

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

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?

  • 1 year later...
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.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.