5

There is a directory where a buddy adds new builds of a product.

The listing looks like this

$ ls path-to-dir/
01
02
03
04
$

where the numbers listed are not files but names of directories containing the builds.

I have to manually go and check every time whether there is a new build or not. I am looking for a way to automate this, so that the program can send an email to some people (including me) whenever path-to-dir/ is updated.

  • Do we have an already existing utility or a Perl library that does this?

    inotify.h does something similar, but it is not supported on my kernel (2.6.9).

I think there can be an easy way in Perl.

  • Do you think this will work?

    Keep running a loop in Perl that does a ls path-to-dir/ after, say, every 5 minutes and stores the results in an array. If it finds that the new results are different from the old results, it sends out an email using Mail or Email.

Lazer
  • 90,700
  • 113
  • 281
  • 364
  • 1
    Why can you not upgrade the kernel - that's pretty old now, the latest stable kernels now is 2.6.34.5 and 2.6.35.4 respectively.... – t0mm13b Sep 11 '10 at 16:28
  • @tommieb75: company machine => not possible. – Lazer Sep 11 '10 at 16:29
  • 2
    Knowing when the build is done will be the toughy. Perhaps `mail -s "build done" Lazer` as the last step in `make build`? – msw Sep 11 '10 at 16:31
  • maybe cron job with simething like make? – flownt Sep 11 '10 at 16:33
  • @Lazer: ahhh ok... company policy so... the easiest is to run a loop and sleep every 5 mins then mail it... you answered yourself! :) Good luck with it. :) – t0mm13b Sep 11 '10 at 16:36
  • I always want to ask in such a case if IT department is there to support you or if you are there to justify IT existence... If you can't update a kernel, file a ticket to your IT and ask them to provide solution. You may just find out that they CAN upgrade a kernel.. (yeah, I am dreaming). – Tomek Sep 11 '10 at 19:38

5 Answers5

5

If you're going for perl, I'm sure the excellent File::ChangeNotify module will be extremely helpful to you. It can use inotify, if available, but also all sorts of other file-watching mechanisms provided by different platforms. Also, as a fallback, it has its own watching implementation, which works on every platform, but is less efficient than the specialized ones.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
rafl
  • 11,980
  • 2
  • 55
  • 77
3

Checking for different ls output would send a message even when something is deleted or renamed in the directory. You could instead look for files with an mtime newer than the last message sent.

Here's an example in bash, you can run it every 5 minutes:

now=`date +%Y%m%d%H%M.%S`

if [ ! -f "/path/to/cache/file" ] || [ -n "`find /path/to/build/dir -type f -newer /path/to/cache/file`" ]
then
    touch /path/to/cache/file -t "$now"
    sendmail -t <<< "
To: aaa@bbb.ccc
To: xxx@yyy.zzz
Subject: New files found

Dear friend,
I have found a couple of new files.
"
fi
Martin
  • 37,119
  • 15
  • 73
  • 82
  • [How would it look like in C shell script](http://stackoverflow.com/questions/3692137/whats-wrong-with-this-c-shell-script)? – Lazer Sep 11 '10 at 19:50
1

Can't it be a simple shell script?

while :;do
        n = 'ls -al path-to-dir | wc -l'
        if n -gt old_n
    # your Mail code here; set old_n=n also
        fi
   sleep 5
done
0

Yes, a loop in Perl as described would do the trick.

You could keep a track of when the directory was last modified; if it hasn't changed, there isn't a new build. If it has changed, an old build might have been deleted or a new one added. You probably don't want to send alerts when old builds are removed; it is crucial that the email is sent when new builds are added.

However, I think that msw has the right idea; the build should notify when it has completed the copy out to the new directory. It should be a script that can be changed to notify the correct list of people - rather than a hard-wired list of names in the makefile or whatever other build control system you use.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

you could use dnotify it is the predecessor of inotify and should be available on your kernel. It is still supported by newer kernels.

David Feurle
  • 2,687
  • 22
  • 38