Script to alert if no new file created in a directory.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Script to alert if no new file created in a directory.
I would like to get an alert whenever there is no new file or directory created over a particular time i.e. for last one hour or a day
Sript 1 is listing the directories declared in script. Taking it little further in script 2 list directories like /u01/log/2021-02. Need Help in script 2 it is failing.
Script 1
#!/bin/bash
arr=(/u01/log/*)
for ((i=0; i < ${#arr[@]}; i++)); do
ls -ld "${arr[$i]}"
done
Success: output -->
/u01/log/log-server1
....
.....
so on..
script 2
#set -xv
#!/bin/bash
MON=date '+%m'
DAY=date '+%d'
YEAR=date '+%Y'
YEAR-MON=date '+%Y-%m'
arr=(/u01/log/*)
for ((i=0; i < ${#arr[@]}; i++)); do
FILE="${arr[$i]}/${YEAR-MON}"
ls -ld $FILE
done
Error: list.sh: line 4: +%m: command not found
list.sh: line 5: +%d: command not found
list.sh: line 6: +%Y: command not found
list.sh: line 7: YEAR-MON=date: command not found
list.sh: line 13: MON: command not found
Once this is resolved it should email alert if no new files created in /u01/log/YEAR-MON
Thanks
Javeed
Last edited by linuxdb@gmail.com; 04-19-2021 at 02:13 AM.
The #!/bin/bash shebang should be the very first line.
As for the multiple calls to date, that must be done using command substitution. Furthermore it should be rolled into one call to avoid the remote possibility of the date changing between separate calls.
As a suggestion this is how I handle my dates for logging and backups. I've used this variable in every script that i've made for the last 5 or 6 years. One little package with all info.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,801
Rep:
Quote:
Originally Posted by linuxdb@gmail.com
I would like to get an alert whenever there is no new file or directory created over a particular time i.e. for last one hour or a day
Sript 1 is listing the directories declared in script. Taking it little further in script 2 list directories like /u01/log/2021-02. Need Help in script 2 it is failing.
You need to either enclose those date commands in backticks or write them as in $(date '+...')
There may be other things that need attention but those just leapt out at me.
What are you looking to do/avoid? Detect whether there's no Oracle activity? You could always use find(1) to print the name of all files created in the previous day and pipe that into "wc -l". If the count is zero, do whatever alerting you need to perform.
Code:
if [ $( find . -type f -mtime 0 -daystart -print | wc -l ) -le 0 ]; then
print "No files found"
fi
(I may have the order of the 'find' mtime and daystart options backwards. Test for yourself to be sure.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.