0

Is it possible to write a cron/script that runs only when there is a change in the folder size .. i.e. the files inside a folders get changed or a new file gets created and hence the folder size would change and the cron or the script would run

TommyT
  • 1,707
  • 3
  • 17
  • 26

3 Answers3

2

There is no support for such a monitor event in standard cron: cron is strictly time-based.

Assuming that cron is used, this task would need to be handled in a "woken up" job, which could then choose to sleep/end immediately or do something else depending on comparing the folder with a previous-known state ..

Now, if cron is removed from the role of being the launch/monitor platform, then there are "non polling" ways to monitor a filesystem such as inotify.

If just looking for a system daemon to supplement standard cron for this task, see the following alternatives.

incron:

incron is an "inotify cron" system. It works like the regular cron but is driven by filesystem events instead of time periods. It contains two programs, a daemon called "incrond" (analogous to crond) and a table manipulator "incrontab" (like "crontab").

Watcher:

Watcher is a daemon that watches specified files/folders for changes and fires commands in response to those changes. It is similar to incron, however, configuration uses a simpler to read ini file instead of a plain text file. Unlike incron it can also recursively monitor directories. It's also written in Python, making it easier to hack.

Community
  • 1
  • 1
user2246674
  • 7,621
  • 25
  • 28
0

Your many solution :

-Using inotifywait, as an example:

inotifywait -m /path 2>&- | awk '$2 == "CREATE" { print $3; fflush() }' |
    while read file; do
        echo "$file"
        # do something with the file
    done

In Ubuntu inotifywait is provided by the inotify-tools package.

-Using incron

You can see a full example here: http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/

-Simple

ls -1A isempty | wc -l 
L. Quastana
  • 1,273
  • 1
  • 12
  • 34
  • Look also this solution it's nice : http://jackal777.wordpress.com/2013/05/11/script-to-monitor-file-creation-under-all-cpanel-users-documentroot/ – L. Quastana Jul 23 '13 at 07:21
0

My idiom is usually:

dir=/dir/to/watch
if [ $dir -nt $dir.flag ]; then
     touch -r $dir $dir.flag
     do_work
fi

This however test against modification time, not size. Size of a directory is not a very useful concept, as it only changes infrequently.

$dir.flag cannot be created in $dir by the way, as this makes $dir change after $dir.flag, so you need to store $dir.flag somewhere where you have write permission.

Coroos
  • 371
  • 2
  • 9