budrito12
asked on
Using UNIX find by time
Hi,
I want to write a UNIX script that checks for any files older then 20mins in
in a unix directory. If it does find a file then an email gets sent notifying the administrator.
If no files are found then it doesn't do anything. Only thing I'm not so sure about writing is the finding of
files which are older then 20mins. I know how to find for files older then 1+ days.
Unix directory = /var/adm/test - check for files here older then 20mins
email = xyz@hotmail.com - email to be sent here
Help would be appreciated in this scripting.
cheers
Bud
I want to write a UNIX script that checks for any files older then 20mins in
in a unix directory. If it does find a file then an email gets sent notifying the administrator.
If no files are found then it doesn't do anything. Only thing I'm not so sure about writing is the finding of
files which are older then 20mins. I know how to find for files older then 1+ days.
Unix directory = /var/adm/test - check for files here older then 20mins
email = xyz@hotmail.com - email to be sent here
Help would be appreciated in this scripting.
cheers
Bud
ASKER
Hi Yuzh,
Thanks for the reply mate but my scripts are after files that are older then 20mins. From what i understand the link http:Q_21074942.html only looks for files newer then the timestamp file. So in otherwords, if the timestamp file time is 14:00 and the current time is 14:20, the script only finds files newer then 14:00. My issue is that there shouldn't be any files older then 15-20 mins in the directory. The script provided in the link will pick up a valid file which may have been created 5min ago and is in the processing stage.
Please advise.
cheers
Thanks for the reply mate but my scripts are after files that are older then 20mins. From what i understand the link http:Q_21074942.html only looks for files newer then the timestamp file. So in otherwords, if the timestamp file time is 14:00 and the current time is 14:20, the script only finds files newer then 14:00. My issue is that there shouldn't be any files older then 15-20 mins in the directory. The script provided in the link will pick up a valid file which may have been created 5min ago and is in the processing stage.
Please advise.
cheers
No problems mate!
For files older that 20mins, you can do:
find . -type f !-newer mystamp -print
!-newer = "NOT newer" = OLDER or the same age
For files older that 20mins, you can do:
find . -type f !-newer mystamp -print
!-newer = "NOT newer" = OLDER or the same age
ASKER
Hi Yuzh,
I'm on TRU64 so it doesn't like the "!-newer" command. Actually your find command errors with the following:
"find: missing conjunction"
So, I removed the -type f (find . !-newer mystamp -print)
This errors with:
find: bad option !-newer
cheers
I'm on TRU64 so it doesn't like the "!-newer" command. Actually your find command errors with the following:
"find: missing conjunction"
So, I removed the -type f (find . !-newer mystamp -print)
This errors with:
find: bad option !-newer
cheers
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yuzh, you're a legend mate. thanks script now works brilliantly. I'II allocate you the points and once again thanks for your help.
cheers
cheers
You are welcome!
Cheers!
Cheers!
ASKER
Hi Yuzh,
One slight problem detected mate. When running the script from command line it runs fine but when scheduled in background I get the following error. I've attached the script as well:
SCRIPT:
#!/bin/ksh
mailgroup=xyz@hotmail.com
LOGFILE=/tmp/logfile
#GET AND PARSE THE CURRENT DATE
date "+%m %d %H %M" | while read MONTH DAY HOURS MINS
do
#SUBTRACT 20 MIN
MINS=$(($MINS-20))
if [ $MINS -lt 0 ]
then
HOURS=$(($HOURS-1))
MINS=$(($MINS+60))
fi
#TOUCH A FILE SO THAT IT IS 20 MINUTES OLD
touch -t ${MONTH}${DAY}${HOURS}${MI NS} /tmp/timestamp
done
#Find files older then 20mins condition
find / ! -newer /tmp/timestamp | while read FILENAME
do
#CHECK THAT IT IS A REGULAR FILE, AND NOT A DIR OR
# CHARACTER FILE, BEFORE RUNNING THE COMMAND AGAINST IT
if [ -f $FILENAME ]
then
echo "$FILENAME" >$LOGFILE
mailx -s "Files have been detected older then 20mins for SIPS in the PROC directory" <$LOGFILE $mailgroup 2>&1
fi
done
The error I get when running from crontab is:
syntax error at line 19: `MIN=$' unexpected
syntax error at line 24: `HOURS=$' unexpected
Any ideas matey.
cheers
Bud
One slight problem detected mate. When running the script from command line it runs fine but when scheduled in background I get the following error. I've attached the script as well:
SCRIPT:
#!/bin/ksh
mailgroup=xyz@hotmail.com
LOGFILE=/tmp/logfile
#GET AND PARSE THE CURRENT DATE
date "+%m %d %H %M" | while read MONTH DAY HOURS MINS
do
#SUBTRACT 20 MIN
MINS=$(($MINS-20))
if [ $MINS -lt 0 ]
then
HOURS=$(($HOURS-1))
MINS=$(($MINS+60))
fi
#TOUCH A FILE SO THAT IT IS 20 MINUTES OLD
touch -t ${MONTH}${DAY}${HOURS}${MI
done
#Find files older then 20mins condition
find / ! -newer /tmp/timestamp | while read FILENAME
do
#CHECK THAT IT IS A REGULAR FILE, AND NOT A DIR OR
# CHARACTER FILE, BEFORE RUNNING THE COMMAND AGAINST IT
if [ -f $FILENAME ]
then
echo "$FILENAME" >$LOGFILE
mailx -s "Files have been detected older then 20mins for SIPS in the PROC directory" <$LOGFILE $mailgroup 2>&1
fi
done
The error I get when running from crontab is:
syntax error at line 19: `MIN=$' unexpected
syntax error at line 24: `HOURS=$' unexpected
Any ideas matey.
cheers
Bud
If you run the script as a cron job, the problem is ENV settings, you can use full path
for all the commands, eg:
/bin/date instead of date
or run the script to use a user's login ENV, eg, you login as fred, and fred can run the
script in commandline.
in your crontab, you do:
/bin/su - fred -c "/path-to/yourscript"
man su
to learn more details.
for all the commands, eg:
/bin/date instead of date
or run the script to use a user's login ENV, eg, you login as fred, and fred can run the
script in commandline.
in your crontab, you do:
/bin/su - fred -c "/path-to/yourscript"
man su
to learn more details.
ASKER
Thanks YUZH, that problem has been solved but another developed. Looking at my script above, if the directory contains 300 files older then 20mins it sends out 300 emails. I onyl want it to send the one single email containing all the 300 file names.
cheers
Bud.
cheers
Bud.
see: http:Q_21057774.html
If not, you need to use touch + find, see:
http:Q_21074942.html