You might be able to do something as simple as:
#!/bin/bash
targetDir="$1"
while true; do
files=("$targetDir"/*)
sleep 30m ## wait 30 minutes
files1=("$targetDir/"*)
if [[ ${#files[@]} == ${#files1[@]} ]]; then
echo "No new files added" | mail user@example.com
fi
done
Save that script as ~/bin/watch.sh
or whatever you like, make it executable (chmod a+x ~/bin/watch.sh
) and run it giving it the target directory as an argument:
watch.sh /path/to/dir
Now, that is a very naive approach and simply counts the number of files every 30 minutes. So if one file is deleted but another one added, it will think there are no new files. It would probably be a better idea to take the timestamp of the newest file and check if it was last modified more than 30 minutes ago:
#!/bin/bash
targetDir="$1"
newest=0;
while true; do
## Wait for half an hour. This is done at the beginning of the loop
## to ensure we wait a half hour before the first check
sleep 30m
## Get the current time in seconds since the epoch
now=$(date '+%s')
for file in "$targetDir"/*; do
## Get the file's modification time in seconds since the epoch
lastMod=$(stat -c "%Y" "$file")
## Is this file newer than the current newest?
if [[ $lastMod -gt $newest ]]; then
newest=$lastMod
fi
done
## If the newest file is older than half an hour
if [[ $((now - newest)) -gt 1800 ]]; then
echo "No new files added since $(date -d '- 30 min')" |
mail user@example.com
fi
done
If you don't want to run a script for ever like that, you could instead write one that checks the timestamps and call the script every 30 minutes:
#!/bin/bash
targetDir="$1"
newest=0;
for file in "$targetDir"/*; do
## Get the file's modification time in seconds since the epoch
lastMod=$(stat -c "%Y" "$file")
## Is this file newer than the current newest?
if [[ $lastMod -gt $newest ]]; then
newest=$lastMod
fi
done
## Get the current time in seconds since the epoch
now=$(date '+%s')
## If the newest file is older than half an hour
if [[ $((now - newest)) -gt 1800 ]]; then
echo "No new files added since $(date -d '- 30 min')" |
mail user@example.com
fi
Now, save that as ~/bin/watch.sh
and make it executable (chmod a+x ~/bin/watch.sh
). Then, run crontab -e
and add this line:
*/30 * * * * /home/techie/bin/watch.sh
Save the file (saving adds the crontab) and you're done. The script will be run every 30 minutes.
cron
to run a script every 30min and then the script itself that will check the directory and send email can be written in pretty any language likebash
,python
orphp
...