Possible to use inotify with unRAID?


Recommended Posts

Windows has the capability to watch files/folders and provide notification if the files/folders change.  Yes, I know the Linux find command, combined with cron, can tell if files are added to a directory.  But what about files being deleted? 

 

It looks like inotify provides similar capability for Linux, but that leaves my with some questions:

1)  Do any of the unRAID distros include inotify as part of the slackware core?

2)  If so, any idea if inotify would work with user shares (and the cache drive)?

3)  Are there better options than inotify?

 

My use case is pretty simple - I want to keep an up-to-date list of media on unRAID that can be queried.

 

Thanks,

Brett

Link to comment
  • 2 weeks later...

I have no need for this, I rsync directories via cron to synchronize them between systems.

 

 

I think the kernel has to be compiled with the subsystem.

Thereafter you build a tool from the library or external tools.

 

I thought these links might be of interest

http://inotify-tools.sourceforge.net/

http://en.wikipedia.org/wiki/Inotify

http://www.ibm.com/developerworks/linux/library/l-inotify.html

 

 

I don't know how valid this is, Yet wonder if having a tool like this running would prevent unmounting of the active filesystems.

 

 

Link to comment

Thanks for the reply WeeboTech.

 

I saw the article at IBM, which is why I was asking about inotify.  I've got unRAID 4.4.2 running, and it doesn't seem to have inotify compiled into the kernel (at least inotify.h is not anywhere on the system).

 

I'm confused about how rsync can provide the capability I was talking about, as I don't want to copy my movies to another location, I just want to keep an up-to-date list of my movies and grab meta-data when new movies are added.  Can you elaborate?

 

As for the unmounting of filesystems, it looks like that isn't a problem.  I found this page, which says cites this as a benefit of inotify over dnotify:

"dnotify requires maintaining an open file descriptor to the directory that the user wants to watch. First, this open file descriptor pins the directory, disallowing the device on which it resides from being unmounted. Second, watching a large number of directories requires too many open file descriptors."

 

"inotify is an inode-based file notification system that does not require a file ever be opened in order to watch it. inotify does not pin filesystem mounts—in fact, it has a clever event that notifies the user whenever a file's backing filesystem is unmounted."

 

I'm not sure how users shares would work, but worst case you could have inotify watch all physical discs (including cache).

 

Brett

Link to comment

I missed the very simple line

 

"My use case is pretty simple - I want to keep an up-to-date list of media on unRAID that can be queried."

 

and

I don't want to copy my movies to another location, I just want to keep an up-to-date list of my movies and grab meta-data when new movies are added.  Can you elaborate?

 

rsync would not be useful for this. Duah..

 

In any case. you always have the ability to use find with the -newer option.

 

I.E.  find (directory) -newer METADATADATABSENAME -exec yourmetaextractionscript

 

The other choice is to start patching the kernel with inotify (unless it's there now.. I have not looked in quote some time).

 

 

 

 

 

Link to comment
Yes, I know the Linux find command, combined with cron, can tell if files are added to a directory.  But what about files being deleted? 

 

find -type f -print | sort >file_list

then, at a later time when you want to see the files deleted since you made the first list

find -type f -print | sort >new_file_list

 

comm -23 file_list new_file_list

The "comm" command by default will output 3 columns:

  lines only in the first list

  lines only in the second list

  lines in both file lists.

 

You can suppress any of the columns by supplying an argument to comm.

comm -23 file_list new_file_list

would suppress the second and third columns leaving you with a list of the files that existed at one time, but no longer exist.

 

Joe L.

Link to comment

Joe,

 

You've got me there.  I had never heard of the comm command before.  You learn something new everyday.

 

Thanks for the tip, although I do still think inotify is a better solution if it is possible (rather than doing a periodic find against all of my media).

 

Brett

Link to comment

Yes, you actually can use inotify with unraid.  I just happened to play with it couple of days before. 

Download it from here http://packages.slackware.it/package.php?q=12.2/inotify-tools-3.13-i486-1 (and select a country near you) to your windows machine.

Then unzip it, and copy from unzipedfolder/usr/bin/* where you will use it (eg /boot/inotify) on unraid, and from unzipedfolder/usr/lib/* to /usr/lib on unraid.

You may alter go script to do that every boot.

After that you can write a small script to run in background that watches your movies directory and exports anything that changed/added to a file.

I am at work right now and cant remember the actual syntax, but it was easy to do that.  If you cant figure it out, post here, and i'll try to find it again for you during the weekend.

 

If these instructions are not the perfect ones, and wget + installpkg can work better, please excuse me (and correct me), i only know about linux, what i read in these forums.

 

Andreas

Link to comment

Yes, you actually can use inotify with unraid.  I just happened to play with it couple of days before. 

Download it from here http://packages.slackware.it/package.php?q=12.2/inotify-tools-3.13-i486-1 (and select a country near you) to your windows machine.

Then unzip it, and copy from unzipedfolder/usr/bin/* where you will use it (eg /boot/inotify) on unraid, and from unzipedfolder/usr/lib/* to /usr/lib on unraid.

You may alter go script to do that every boot.

After that you can write a small script to run in background that watches your movies directory and exports anything that changed/added to a file.

I am at work right now and cant remember the actual syntax, but it was easy to do that.  If you cant figure it out, post here, and i'll try to find it again for you during the weekend.

 

If these instructions are not the perfect ones, and wget + installpkg can work better, please excuse me (and correct me), i only know about linux, what i read in these forums.

 

Andreas

It is much easier to put the downloaded .tgz file on your flash drive and then use the installpkg command to install it.  It automatically puts the files in the correct locations.

 

First, create a "packages" folder at the root of your flash drive.  On Linux this would be at /boot/packages

Next, download from a site in/near your country from the link above.  Put the file in the "packages" folder.

Then... to install, log in via telnet, or on the system console and type:

cd /boot/packages

installpkg inotify-tools-3.13-i486-1.tgz

 

You still need to configure inotify and invoke it on the files and folders you wish to monitor..

 

If you wish to perform the download of the package using "wget" on Linux rather than using your browser on your PC, you can use this command to get the file from one of the slackware mirror sites.

cd /boot/packages

wget  http://slackware.cs.utah.edu/pub/slackware/slackware-12.2/slackware/a/inotify-tools-3.13-i486-1.tgz

 

Now... some good news, and some bad news

I installed it on my server to see how it worked and then invoked it like this on my Movies share

root@Tower:/boot/packages# inotifywait -m -r /mnt/user/Movies

Setting up watches.  Beware: since -r was given, this may take a while!

Couldn't watch /mnt/user/Movies: No such file or directory

 

It does not recognize the user share file-system folder as a valid folder. (Yes, /mnt/user/Movies exists)

 

I then tried it on my disk hierarchy:

root@Tower:/boot/packages# inotifywait -m -r /mnt/disk*/Movies

Setting up watches.  Beware: since -r was given, this may take a while!

Watches established.

/mnt/disk2/Movies/ OPEN,ISDIR

/mnt/disk2/Movies/ CLOSE_NOWRITE,CLOSE,ISDIR

/mnt/disk2/Movies/ OPEN,ISDIR Demo-Disks

/mnt/disk2/Movies/Demo-Disks/ OPEN,ISDIR

/mnt/disk2/Movies/ CLOSE_NOWRITE,CLOSE,ISDIR Demo-Disks

/mnt/disk2/Movies/Demo-Disks/ CLOSE_NOWRITE,CLOSE,ISDIR

/mnt/disk4/Movies/ OPEN,ISDIR

/mnt/disk4/Movies/ CLOSE_NOWRITE,CLOSE,ISDIR

/mnt/disk4/Movies/ OPEN,ISDIR Trailers

/mnt/disk4/Movies/Trailers/ OPEN,ISDIR

/mnt/disk4/Movies/ CLOSE_NOWRITE,CLOSE,ISDIR Trailers

/mnt/disk4/Movies/Trailers/ CLOSE_NOWRITE,CLOSE,ISDIR

/mnt/disk4/Movies/ OPEN,ISDIR Trailers

/mnt/disk4/Movies/Trailers/ OPEN,ISDIR

/mnt/disk4/Movies/ CLOSE_NOWRITE,CLOSE,ISDIR Trailers

/mnt/disk4/Movies/Trailers/ CLOSE_NOWRITE,CLOSE,ISDIR

/mnt/disk4/Movies/Trailers/ OPEN T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ ACCESS T6-MovieTrailers.iso

/mnt/disk4/Movies/Trailers/ CLOSE_NOWRITE,CLOSE T6-MovieTrailers.iso

 

That worked...  The multiple ACCESS lines occurred as the MovieTrailers.ISO file was played. 

 

I see this being very helpful to learn what is spinning up our drives...  You can monitor specific events by adding arguments to the command line.  The manual page is here: http://linux.die.net/man/1/inotifywait

 

Joe L.

 

 

Link to comment

Joe thanks for the installation tip.  i had already download and extract it to my windows machine, so i didnt gave it a try.

You are also right about the inability of the inotify to recognize the fuse paths.  I had the same problem and used the /mnt/disk* trick.

One thing that also tried, but it took too much time and i didnt let it finish was the following.

cat /proc/sys/fs/inotify/max_user_watches

This gave me 8192, wich is the maximum number of files to watch.  So i typed

echo 1000000 > /proc/sys/fs/inotify/max_user_watches

to increase number of files to 1 million

Then i tried to call inotify at /mnt and it started scanning forever, so after one hour i stopped it (so i dont know if it actually works for the whole unraid)

The command i used to see what is changed is

inotifywait -mrqce close_write -e moved_to -e moved_from -e delete -e unmount /mnt/disk*/Videos | awk -F , '{print $1$(NF)}'

and with the addition of

> /tmp/somefile you could have it to a file

(ok i understand that all that is trivial, but coming from windows, is like i rediscover computers as it should be  :)  - please dont flame me )

 

ps. Please, excuse my abuse of English, it is not my native language

 

Andreas

Link to comment
  • 2 weeks later...

OK I am trying to get this up and running on my machine and for the most part it seems to work.  There is one question I have for the more Linux savvy crowd though.

 

I have the Third party Boot Flash Plugin Architecture setup and am trying to keep my go script as clean as possible so I have a file (inotify) that looks like this:

 

cd /boot/packages
installpkg inotify-tools-3.13-i486-1.tgz

inotifywait -m /mnt/disk*/DVD
inotifywait -m -r /mnt/disk1/torrents
inotifywait -m -r /mnt/disk1/Anime
inotifywait -m -r /mnt/disk1/TV\ Shows

 

The above file gets run by my rc.local_startup file (which I found on here).

 

Now my question is how can I delay the start of the inotify file so that the rest of my system can load and not be taken over while inotifywait sets up all the watches.  Would I end up making my file look something like:

 

cd /boot/packages
installpkg inotify-tools-3.13-i486-1.tgz

inotifywait -m /mnt/disk*/DVD [color=red][b]| at now + 5 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/torrents [color=red][b]| at now + 10 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/Anime [color=red][b]| at now + 15 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/TV\ Shows [color=red][b]| now + 20 minutes[/b][/color]

 

Also, for my DVD directory I have all of those as .iso files.  Would it be necessary to add the -r switch for that directory seeing as there are no other folders in there?  All the other ones I want to be watched do indeed have other folders that need watched but the DVD one does not.

 

Thanks in advance

Link to comment

cd /boot/packages
installpkg inotify-tools-3.13-i486-1.tgz

inotifywait -m /mnt/disk*/DVD [color=red][b]| at now + 5 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/torrents [color=red][b]| at now + 10 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/Anime [color=red][b]| at now + 15 minutes[/b][/color]
inotifywait -m -r /mnt/disk1/TV\ Shows [color=red][b]| [color=maroon]at[/color] now + 20 minutes[/b][/color]

 

You syntax is close... but not quite there

it should be:

echo "inotifywait -m /mnt/disk*/DVD" | at now + 5 minutes

echo "inotifywait -m -r /mnt/disk1/torrents" | at now + 10 minutes

echo "inotifywait -m -r /mnt/disk1/Anime" | at now + 15 minutes

echo "inotifywait -m -r /mnt/disk1/TV\ Shows" | now + 20 minutes

 

You need to "echo" the commands you wish executed later to the standard input of  the "at" command.

 

And you are correct, you do not need the -r option on the DVD folder if there are no subfolders... but it would not hurt either.

 

Joe L.

 

Link to comment

You syntax is close... but not quite there

it should be:

echo "inotifywait -m /mnt/disk*/DVD" | at now + 5 minutes

echo "inotifywait -m -r /mnt/disk1/torrents" | at now + 10 minutes

echo "inotifywait -m -r /mnt/disk1/Anime" | at now + 15 minutes

echo "inotifywait -m -r /mnt/disk1/TV\ Shows" | now + 20 minutes

 

You need to "echo" the commands you wish executed later to the standard input of  the "at" command.

 

And you are correct, you do not need the -r option on the DVD folder if there are no subfolders... but it would not hurt either.

 

Joe L.

 

Thanks Joe, I though I was close but not quite right.

 

The problem that I am having is that when I run inotifywait -m -r /mnt/disk*/DVD is takes over and the server does not fully boot.  I got stuck with the main part of the system loading but not some of the other stuff.

Link to comment

You syntax is close... but not quite there

it should be:

echo "inotifywait -m /mnt/disk*/DVD" | at now + 5 minutes

echo "inotifywait -m -r /mnt/disk1/torrents" | at now + 10 minutes

echo "inotifywait -m -r /mnt/disk1/Anime" | at now + 15 minutes

echo "inotifywait -m -r /mnt/disk1/TV\ Shows" | now + 20 minutes

 

You need to "echo" the commands you wish executed later to the standard input of  the "at" command.

 

And you are correct, you do not need the -r option on the DVD folder if there are no subfolders... but it would not hurt either.

 

Joe L.

 

Thanks Joe, I though I was close but not quite right.

 

The problem that I am having is that when I run inotifywait -m -r /mnt/disk*/DVD is takes over and the server does not fully boot.  I got stuck with the main part of the system loading but not some of the other stuff.

If you run it before unRAID has set up it shares it might interfere...  Are you running it at the command line? or via "at" after the array is on-line?

 

Also, inotify uses up system resources... specifically memory.  Are you sure you are not running out of ram?  (How much memory do you have? and how many directories/files is it monitoring?)

 

As I'm sure you know, you are one of the first to experiment with this on an unRAId server. 

 

When you say the server does not fully boot, can you be a bit more explicit where it stops, or what does not fully boot? 

 

Where are you sending the output of inotifywait?  It has tons of output as the file-system is accessed.  Perhaps the output is so huge it is filling memory as the entire directory tree is accessed by the user-shares as they are initially set up.

 

You might try

echo "inotifywait -m /mnt/disk*/DVD >/mnt/disk2/inotify_out.txt 2>&1" | at now + 5 minutes

To send the output elsewhere. (On a different disk, so it does not end up in a loop reporting on its own writes)

 

Joe L.

Link to comment

If you run it before unRAID has set up it shares it might interfere...  Are you running it at the command line? or via "at" after the array is on-line?

I run BubbaRaid also, along with unMenu and maybe a few others.  The problem that I was having is that BubbaRaid did not load, nor did unMenu.  I got the main unRAID web interface loaded along with all the user and disk shares but really nothing else.

 

Also, inotify uses up system resources... specifically memory.  Are you sure you are not running out of ram?  (How much memory do you have? and how many directories/files is it monitoring?)

I have 2GB of memory in the machine and to be honest I am not sure if I am running out of ram.  It is monitoring 4 directories all told,

  • Directory 1 has no sub folders and 190 files
  • Directory 2 has 3 sub folders that have no folders; there are ~330 files all told
  • Directory 3 has 4 sub folders that have 24 sub folders, these 24 have no folders; there are ~512 files all told
  • Directory 4 has 3 sub folders that have ~5-10 sub folders, these ~5-10 have no folders; there are ~30-40 files all told

 

As I'm sure you know, you are one of the first to experiment with this on an unRAId server. 

Yup fully understand that and to be honest I am not sure it will be totally useful for me but I figured I would try it out for the hell of it.  I have a couple room mates that I allow to use it and save stuff to it and I figured that I might just want to monitor what they are doing with it.  It is kinda big brother-ish but I am the one that paid for it and set it up so...

 

When you say the server does not fully boot, can you be a bit more explicit where it stops, or what does not fully boot? 

See above for explination of what the server is doing.

 

Where are you sending the output of inotifywait?  It has tons of output as the file-system is accessed.  Perhaps the output is so huge it is filling memory as the entire directory tree is accessed by the user-shares as they are initially set up.

This was kinda what I was thinking and was going to reboot the server soon so that I could redirect this to a text file and see what was being done.

 

You might try

echo "inotifywait -m /mnt/disk*/DVD >/mnt/disk2/inotify_out.txt 2>&1" | at now + 5 minutes

To send the output elsewhere. (On a different disk, so it does not end up in a loop reporting on its own writes)

 

That is the next step that I was planning on taking.  I have a share on the server that no one else can see or access except for me and I am probably going to redirect the output to there.  I was going to do something like:

echo "inotifywait -m /mnt/disk*/DVD [color=blue]>/mnt/disk1/Kyles\ Files/inotify_DVD.txt 2>&1[/color]" | at now + 5 minutes
echo "inotifywait -m -r /mnt/disk1/torrents [color=blue]>/mnt/disk1/Kyles\ Files/inotify_torrents.txt 2>&1[/color]" | at now + 10 minutes
echo "inotifywait -m -r /mnt/disk1/Anime [color=blue]>/mnt/disk1/Kyles\ Files/inotify_Anime.txt 2>&1[/color]" | at now + 15 minutes
echo "inotifywait -m -r /mnt/disk1/TV\ Shows [color=blue]>/mnt/disk1/Kyles\ Files/inotify_TV_Shows.txt 2>&1[/color]" | now + 20 minutes

question: in the above do I need the "Kyles\ Files" or since it is in quotes I can just put it as "Kyles Files"

 

 

If there is any command you would like me to run via telnet let me know and I will report my findings as best I can.

Link to comment
  • 2 weeks later...

OK, for anyone wondering I have this up and running on my system with success.  Joe. L's suggestion of using at works great.

 

the script setup I use is below:

 

cd /boot/packages
installpkg inotify-tools-3.13-i486-1.tgz
echo "inotifywait -m -r /mnt/disk1/TV\ Shows >/mnt/disk1/Kyles\ Files/inotify_TV_Shows.txt 2>&1" | at now + 5 minutes
echo "inotifywait -m -r /mnt/disk1/Anime >/mnt/disk1/Kyles\ Files/inotify_Anime.txt 2>&1" | at now + 10 minutes
echo "inotifywait -m -r /mnt/disk1/torrents >/mnt/disk1/Kyles\ Files/inotify_torrents.txt 2>&1" | at now + 15 minutes
echo "inotifywait -m /mnt/disk*/DVD >/mnt/disk1/Kyles\ Files/inotify_DVD.txt 2>&1" | at now + 20 minutes

 

The only problem I am having seems to be the fact that the "TV Shows" folder does not seem to be getting scanned, not quite sure why.  The others Anime, torrents, and DVD are doing just fine.  I highly suggest that anyone running this redirect the output like I did to a text file.  I save mine to a share on the server that only I can get to.

 

If anyone has any other questions let me know and I will do my best to answer.

Link to comment

I started this thread, but you've done all of the work.  Thanks, I really appreciate your efforts.

 

In terms of the TV Shows not working, only two thoughts come to mind.  1st, if you have any syntax or spelling error, so you're not watching what you intend?  Unlikely, but possible.  2nd, since TV Shows is the first line, it is executed earliest.  Is it possible that the system isn't ready for 7 minutes, so the 1st line doesn't work, but the rest do?  Maybe bumping all the times up by 5 would help?

 

[edit - robj beat me to the punch - this is a reply to prostuff1's post]

 

Brett

Link to comment

Is the at command missing in the TV Shows line?

 

Also, would it be safer, or is it unnecessary, to put single quotes around the file and folder names with spaces (eg. '/mnt/disk1/TV Shows' and '/mnt/disk1/Kyles Files/inotify_Anime.txt')?

 

I hate when I do crap like that.

 

Thanks for pointing out the mistake.  I will fix it and let you know what comes of it.  I have a feeling I will work after I correct my mistake.

Link to comment

Quick question for all of those linux gurus out there:

 

Since these files (the .txt ones) can already exist on a reboot i need a way to either appened any new stuff to the end, or overwrite the file that exists.

 

I am fairly certain that i could just add an:

rm /mnt/disk1/Kyles\ Files/inotify_TV_Shows.txt
rm /mnt/disk1/Kyles\ Files/inotify_Anime.txt
rm /mnt/disk1/Kyles\ Files/inotify_DVD.txt

to the start of the script but i think there is an easier way, by adding a ! somewhere in the original script below.

 

cd /boot/packages
installpkg inotify-tools-3.13-i486-1.tgz
echo "inotifywait -m -r /mnt/disk1/TV\ Shows >/mnt/disk1/Kyles\ Files/inotify_TV_Shows.txt 2>&1" | at now + 10 minutes
echo "inotifywait -m -r /mnt/disk1/Anime >/mnt/disk1/Kyles\ Files/inotify_Anime.txt 2>&1" | at now + 15 minutes
echo "inotifywait -m /mnt/disk*/DVD >/mnt/disk1/Kyles\ Files/inotify_DVD.txt 2>&1" | at now + 20 minutes

 

For anyone wondering, i took the torrents directory out of the watch list as all it does is get constantly updated.  I should have thought of that before i set it up but once i did the redirect to a file it constantly grows and gets bigger.

 

If anyone is thinking of using this I highly suggest redirecting the output to a txt file for easier reading and so the RAM on the system does not get used up.  My torrents txt file was well over 200MB when i stopped it and rebooted the server.

Link to comment

Quick question for all of those linux gurus out there:

 

Since these files (the .txt ones) can already exist on a reboot i need a way to either appened any new stuff to the end, or overwrite the file that exists.

Since you are using ">" to direct the output of inotifywait and old contents will be truncated.  You do not need to worry about them growing forever.

 

If you wanted to always "append" to a file, you would use ">>" instead of ">"

 

I think your commands will work as you desire as you have them.

 

Joe L.

Link to comment

Here is a nice little script I put into place on my server.  It uses inotifywait to monitor the accesses of the user-share directories.

When an access of the directory is detected (usually because we are browsing the listing from a media player) the unRAID management interface is accessed and used to spin-up all the drives.

 

I know it won't work for everybody, and many will not want to spin up all the drives, but it works for my situation.  If another directory access is detected and it has been more than 50 minutes, it will spin up the drives once more.. (keeping them spinning)  It just sits there doing nothing at all if you are playing a movie.  It only cares about directory listings, and attempts to un-mount user-shares (if a un-mount occurs, it schedules itself to try again later via the "at" command and exits)

 

You do need to edit it to have the names of the user-shares you want to watch... and you do need a inotify-tools package in your /boot/packages directory so it can install it when it runs. (it only installs it if it is not already installed)

 

This is a proof of concept... for the more experienced, since it needs some editing to on your server.

 

Have fun...

 

Joe L.

[pre]

#!/bin/bash

# spinup_when_accessed.sh

# version 1.0

#

# Use inotify to spin up sleeping drives when a user share

# directory is listed by a media player.

#

 

# Modify the following list of folders to watch as needed.  Shell expansion is supported.

# (use "spaces" between folder names)

watch_dirs='/mnt/user/Movies* /mnt/user/Pictures'

 

# Modify if needed to full path to inotify-tools package.

inotify_tools="/boot/packages/inotify-tools-3.13-i486-1.tgz"

 

if [ ! -f "$inotify_tools" ]

then

  echo "Error: $inotify_tools installable package does not exist" >&2

  exit 2

fi

 

# Wait until user-shares exist, try again in 2 minutes

if [ ! -d /mnt/user ]

then

  echo $0 "$@" | at now + 2 minutes

  exit

fi

program_name=`basename $0`

quit_flag="no"

 

if [ "$1" = "-q" ]

then

  quit_flag="yes"

fi

 

lockfile="/var/lock/spinup_when_accessed.LCK"

if [ -f "${lockfile}" ]; then

 

  # The file exists so read the PID

  # to see if it is still running

  lock_pid=`head -n 1 "${lockfile}"`

 

  pid_running=`ps -p "${lock_pid}" | grep ${lock_pid}`

 

  if [ -z "${pid_running}" ]; then

    if [ "$quit_flag" = "no" ]

    then

      # The process is not running

      # Echo current PID into lock file

      echo $$ > "${lockfile}"

    else

      echo "$program_name ${lock_pid} is not currently running "

      rm "${lockfile}"

      exit 0

    fi

  else

  if [ "$quit_flag" = "yes" ]

    then

      echo killing $program_name process "$lock_pid"

      echo killing $program_name process "$lock_pid" | logger -t$program_name

      rm "${lockfile}"

      killall inotifywait

      kill "$lock_pid"

      exit 0

    else

      echo "$program_name is already running [${lock_pid}]"

      exit 2

    fi

  fi

else

  if [ "$quit_flag" = "yes" ]

  then

    echo "$program_name not currently running "

    exit 0

  else

    echo $$ > "${lockfile}"

  fi

fi

 

# If not already installed, install the inotify-tools package.

which inotifywait >/dev/null 2>&1 || {

  # Install the inotify tools package.  It will provide output when share folders are accessed.

  installpkg "$inotify_tools"

}

 

# pretend the last time we spun up was in 1970. (zero seconds)

last_time=0

parent_pid=$$

 

inotifywait -m $watch_dirs  | while read a

do

    case $a in

    *UNMOUNT*)

      # if we detect an un-mount attempt, exit and try again in 5 minutes.

      # Kill the parent process, which should also terminate the inotifywait.

      echo $0 "$@" | at now + 5 minutes

      kill $parent_pid

      exit

    ;;

    *ISDIR*)

        current_time=`date +%s`

        elapsed_time=$(expr $current_time - $last_time)

        # A watched directory is being accessed.

        # If it has been more than 50 minutes since we last issued a Spin-Up

        # command, issue one.  Somebody is doing a directory listing, and odds

        # are they will want to access a file in one of the directories.

        if [ $elapsed_time -gt 3000 ]

        then

            wget -S localhost/main.htm?cmdSpinUpAll=Spin%20Up -o /tmp/spin_up

            last_time=$current_time

        fi

    ;;

    esac

    if [ ! -f $lockfile ]

    then

      kill $parent_pid

      exit

    fi

done

[/pre]

 

Link to comment

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.