Inotifyd: Difference between revisions

From Alpine Linux
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Use case ==
== Use case ==


Sometimes we need to monitor a directory (or file) and do some post processing/actions. Instead of polling the directory for changes we can use inotify to watch a specific folder or file and be notified if something happens within its context. While there are tools in aports designed around inotify (inotify-tools) alpine has a build in tools called inotifyd (part of busybox) to execute command. Below is an example of how you can easily setup a watch folder with just base alpine (busybox and openrc).
Sometimes we need to monitor a directory (or file) and do some post processing/actions. Instead of polling the directory for changes we can use inotify to watch a specific folder or file and be notified if something happens within its context. While there are tools in aports designed around inotify (inotify-tools) alpine has a build in tool called inotifyd (part of busybox) to execute a command on file system events. Below is an example of how you can setup a watch folder with just base alpine (busybox and openrc).


=== Create an openrc service script ===
== Create an OpenRC service script ==


==== /etc/init.d/inotifyd ====
 
<pre>
{{Cat|/etc/init.d/inotifyd|<nowiki>#!/sbin/openrc-run
#!/sbin/openrc-run


command=/sbin/inotifyd
command=/sbin/inotifyd
Line 19: Line 18:
         checkpath --directory --owner $INOTIFYD_USER --mode 0775 \
         checkpath --directory --owner $INOTIFYD_USER --mode 0775 \
                 /var/log/$RC_SVCNAME
                 /var/log/$RC_SVCNAME
}
}</nowiki>}}
</pre>
 
 
{{Cat|/etc/conf.d/inotifyd|<nowiki>INOTIFYD_ARGS="/usr/local/bin/myscript /home/username/watchdir:w"
INOTIFYD_USER=username</nowiki>}}


==== /etc/conf.d/inotifyd ====
<pre>
INOTIFYD_ARGS="/usr/local/bin/myscript /home/username/watchdir:w"
INOTIFYD_USER=username
</pre>


==== /usr/local/bin/myscript ====
{{Cat|/usr/local/bin/myscript|<nowiki>#!/bin/sh
<pre>
#!/bin/sh


event="$1"
event="$1"
Line 40: Line 35:
   w) echo "writable $file is closed in $directory";;
   w) echo "writable $file is closed in $directory";;
   *) echo "This will never happen as we only listen for w.";;
   *) echo "This will never happen as we only listen for w.";;
esac
esac</nowiki>}}


</pre>
== Finishing up ==
 
=== Finishing up ===


Starting inotifyd:
Starting inotifyd:


{{Cmd|rc-service inotifyd start}}
{{Cmd|# rc-service inotifyd start}}


Now copy some file into the watch folder and check the service log in /var/log/inotifyd
Now copy some file into the watch folder and check the service log in {{Path|/var/log/inotifyd}}.


In the example above we only listen for file closed events (w). In case you want to listen to more events you can just add them after each other. In case you want to listen to all of them you simply remove all appending event letters from the directory.
In the example above we only listen for file closed events (w). In case you want to listen to more events you can just add them after each other. In case you want to listen to all of them you simply remove all appending event letters from the directory.


=== Possible inotify Events ===
== Possible inotify Events ==


<pre>
<pre>
Line 75: Line 68:
         d      Subfile is deleted
         d      Subfile is deleted
</pre>
</pre>
[[Category:Monitoring]]

Latest revision as of 19:52, 1 June 2023

Use case

Sometimes we need to monitor a directory (or file) and do some post processing/actions. Instead of polling the directory for changes we can use inotify to watch a specific folder or file and be notified if something happens within its context. While there are tools in aports designed around inotify (inotify-tools) alpine has a build in tool called inotifyd (part of busybox) to execute a command on file system events. Below is an example of how you can setup a watch folder with just base alpine (busybox and openrc).

Create an OpenRC service script

Contents of /etc/init.d/inotifyd

#!/sbin/openrc-run command=/sbin/inotifyd command_args="$INOTIFYD_ARGS" command_user="$INOTIFYD_USER" pidfile=/run/${RC_SVCNAME}.pid command_background=yes start_stop_daemon_args="--stdout /var/log/$RC_SVCNAME/${RC_SVCNAME}.log --stderr /var/log/$RC_SVCNAME/${RC_SVCNAME}.log" start_pre() { checkpath --directory --owner $INOTIFYD_USER --mode 0775 \ /var/log/$RC_SVCNAME }


Contents of /etc/conf.d/inotifyd

INOTIFYD_ARGS="/usr/local/bin/myscript /home/username/watchdir:w" INOTIFYD_USER=username


Contents of /usr/local/bin/myscript

#!/bin/sh event="$1" directory="$2" file="$3" # run some command based on an event case "$event" in w) echo "writable $file is closed in $directory";; *) echo "This will never happen as we only listen for w.";; esac

Finishing up

Starting inotifyd:

# rc-service inotifyd start

Now copy some file into the watch folder and check the service log in /var/log/inotifyd.

In the example above we only listen for file closed events (w). In case you want to listen to more events you can just add them after each other. In case you want to listen to all of them you simply remove all appending event letters from the directory.

Possible inotify Events

Events:
        a       File is accessed
        c       File is modified
        e       Metadata changed
        w       Writable file is closed
        0       Unwritable file is closed
        r       File is opened
        D       File is deleted
        M       File is moved
        u       Backing fs is unmounted
        o       Event queue overflowed
        x       File can't be watched anymore
If watching a directory:
        y       Subfile is moved into dir
        m       Subfile is moved out of dir
        n       Subfile is created
        d       Subfile is deleted