LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2021, 02:11 AM   #1
linuxdb@gmail.com
LQ Newbie
 
Registered: Apr 2021
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 04-19-2021, 02:20 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.

Code:
#!/bin/bash
# set -xv
d=$(date +"%F")
MON=$(date -d $d +'%m')
DAY=$(date -d $d +'%d')
YEAR=$(date -d $d +'%Y')
YEARMON=$(date -d $d +'%Y-%m')
...
Hyphens are not allowed in variable names in shell scripts. See also the online shell script analysis tool ShellCheck.
 
Old 04-19-2021, 09:19 AM   #3
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.

Code:
NOW=$(date +%Y.%m.%d.%H.%M)
 
Old 04-20-2021, 03:47 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP Please start here:
https://www.linuxquestions.org/quest...gs-4175464257/
 
Old 04-20-2021, 07:30 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by Turbocapitalist View Post
Hyphens are not allowed in variable names in shell scripts.
The things you learn late at night watching the footy ...
 
Old 04-20-2021, 09:46 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Find files or directories from today (last 24 hours)
Code:
find /u01/log/????-?? -mtime 0
The find has no meaningful exit status, but grep has
Code:
if find /u01/log/????-?? -mtime 0 | grep -q .
then
  : files found
else
  echo "no files found"
fi
 
Old 04-21-2021, 12:11 AM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,801

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by linuxdb@gmail.com View Post
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.)


HTH...

Last edited by rnturn; 04-21-2021 at 12:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check what files was created. Monit alert, strange alert g_paschoal Linux - Server 1 01-21-2016 12:24 PM
OPENNMS -how can i get disk space alert & memory alert BY MAIL saravanakumar Linux - Server 11 05-30-2014 08:45 AM
opennms --memory alert & disk alert issue saravanakumar Linux - Server 4 07-19-2011 10:54 AM
ALERT!!! ALERT!!! I messed up the UNIX!!! Firew Linux - Software 1 11-05-2001 11:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration