Views:
5,790β
Votes: 2β
Tags:
command-line
bash
scripts
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1157136
Title:
How I can monitor for new files in a directory and drop a mail if files are not added every 30 mins?
ID:
/2019/07/09/How-I-can-monitor-for-new-files-in-a-directory-and-drop-a-mail-if-files-are-not-added-every-30-mins_
Created:
July 9, 2019
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
The answers so far assume that files only go into a directory and never go out. But what if you have a script that empties the directory periodically? Then the script could miss the fact a file went into the directory 5 minutes ago but was processed and taken out.
Another option is using the inotifywait
command written in highly efficient C language for the express purposes of monitoring file and directory creation and modification. You can install it with the command:
sudo apt install inotify-tools
The psuedo-code for an appropriate script would be:
- Wait 30 minutes or until directory changes
- Did directory change?
- Yes? We got here because directory changed. Loop back and wait 30 minutes again.
- No? We got here because 30 minutes expired. Send email and loop back to wait 30 minutes again.
You would call the script from a place like /etc/rc.local
so it runs when machine is booted.
The advantage of this methodology is you arenβt checking the directory every 30 minutes but rather you are checking it 30 minutes after the last changes to the directory.