Suppose you want your Solaris shell script to take some kind of action when a new file arrives in a certain directory. Traditionally this has been done by periodically polling the directory. Not very elegant and what is worse: it uses unnecessary resources.
Linux has the inotify kernel subsystem and on many distros you’ll also find the inotify-tools so that you can use this from the command line and thus in a script. You would typically use the
inotifywait command in scripts to cause a blocking wait for some file event to happen.
So what about Solaris ?
The equivalent of Linux’s inotify in Solaris is called File Events Notifications but the caveat is that although it was created in 2007 it didn’t make it into Solaris until version 11. So you’ll need Solaris v11.0 or Solaris v11.1 or later.
In Solaris 11.x there’s is no equivalent to the inotify-tools but it is extremely easy to create so this is what I’ll do here.
I’ve created a small tool that I call watchdir (see source below). It can be used as a command in a script and takes two arguments namely the name of the directory to watch and optionally a timeout. Feel free to steal it and elaborate on it.
will wait indefinitely for changes to the
/foo/bar directory. When a change happens the command will return. See return codes in source code below.
Compile and build
This is the easy part. All you have to do is this:
First take the C source code that you find on this page and put it into a new file called
(if using GNU C/C++ Compiler)
pkg install pkg://solaris/developer/gcc-45 pkg install pkg://solaris/system/header gcc watchdir.c -o watchdir
(if using Solaris Studio Compiler)
# Install steps for Solaris Studio compiler not shown here cc watchdir.c -o watchdir
After this you now have a binary called ‘watchdir’ that you can use in scripts, etc. It is pretty self-explanatory. Have a look at the header of the source code which explains how to use it.
Here’s a Bash example of how watchdir can potentially be used from within a shell script.
There are many extensions that can be done from this and certainly
watchdir is nowhere as flexible as the
inotifywait command in Linux. Having said that it is more than sufficient for my purpose. I haven’t actually had a need for something more advanced.
It currently triggers on FILE_MODIFIED or FILE_ATTRIB (source code line 207). In this case remember that ‘file’ is really a directory, because that is what we’re watching. If somebody puts a new file into the directory or deletes a file from the directory this will modify the directory and since we’re subscribing to ‘MODIFIED’ events we’ll get a callback. The reason for also using FILE_ATTRIB eludes me (it is some months since I wrote this) but I guess the thinking was that I also wanted to be notified if somebody made the directory read-only .. or something. You can play with this and find the combination that fits your purpose. Or you could change the source code and have such options as part of the command line input.
The source code is highly inspired / stolen from Prakash Sangappa’s blog. I’ve cleaned it up, made some more source code comments and most of all I’ve made it operational as a command line tool. Feel free to grab it, steal it, etc. And Oracle if you’re listening: I would like to see something like this become part of core Solaris.