Need advice on writing a script to check if a file exists and send notification
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
Please, don't demand urgent help. LinuxQuestions is a community of volunteers that freely spend some of their time to help people. Demanding urgent help is somewhat considered rude.
Moreover, I think you should show us a minimum effort, especially if it is homework. Post the code you've written so far, explain where you're stuck and if you have some specific question feel free to ask.
if [ ! -f "/opt/oracle/admin/maintenance/reporting_schedule/status/flag/EDS_report_ready.flag" ]
then
echo "$filename does not exists in the current directory" | mailx -s 'stats' santosh@mail.com
else
echo "$filename does exists in the current directory" | mailx -s 'stats' santosh@mail.com
fi
this script what i written.. i dont know weather its correct or not.
Will u please check it once and tel m wat changes hav 2 do..
The syntax is correct. The only doubt is about the filename variable: you use it in the body of the if/then/else construct but not in the conditional expression. Moreover, maybe the posted code is only a part of a larger script, but it's a good habit to put a sha-bang, e.g.
Code:
#!/bin/bash
in the very first line of the script, to tell the system which interpreter must be used to execute the command statements.
Here's something similar that I use, my requirement is slightly different in that I'm only interested if the file is more than a number of minutes old, but it can be modified so that if you set AGE to one minute then it'll pretty much just check for a file existing. It also counts the number of files that match the FMASK and AGE criteria so it can be used for a few purposes. We use it to look for aged lock files, we have a process that runs every 5 minutes so if the lock file is more than 15 minutes old then the process is considered "stale" so we need an e-mail alert.
The additional headers mark and tag the e-mail in MS Outlook and other mail clients.
Code:
#!/bin/bash
# Author: Grant MacDonald
# Purpose: Check for aged file over a number of minutes old.
# This script actually counts the number of files that are
# older than the specified age rather than looking for a
# single file. However if the FMASK isn't "wildcarded" then
# it'll only look for one file.
# Version: 1.1
# Date: 10/08/2011
# Version History
# 1.0 Initial version
# 1.1 Changed e-mail address to send notifications TO
# How old must the file be to cause an alert?
AGE=1
# Number of files
AGEDCOUNT=0
# Folder/Filemask we're interested in
FILEDIR=""/opt/oracle/admin/maintenance/reporting_schedule/status/flag/"
FMASK="EDS_report_ready.flag"
# Mail Notification
MAILTO="yourmail@yourdomain.com"
MAILSUBJECT="Alert - Aged ${FMASK} found."
MAILBODY="File ${FILEDIR}${FMASK} more than ${AGE} minutes old.\n\n"
MAILFROM="frommail@yourdomain.com"
MAILFILE="/tmp/monitor${FMASK}.eml"
# MAIN SCRIPT LOGIC STARTS HERE
# Clear our errorlevel
ERRORLEVEL=0
# What server are we running on?
NODE=`/bin/uname -n`
# We're really only interested in returning an error if we've
# files more than ${AGE} minutes old
AGEDCOUNT=`/bin/find ${FILEDIR} -maxdepth 1 -name ${FMASK} -mmin +${AGE} | /usr/bin/wc -l`
if [ "${AGEDCOUNT}" -eq "0" ] ; then
ERRORLEVEL=0
else
ERRORLEVEL=${AGEDCOUNT}
fi
if [ "${ERRORLEVEL}" -gt "255" ] ; then
ERRORLEVEL=254
fi
if [ "${ERRORLEVEL}" -ne "0" ] ; then
# Notification Logic Goes Here
echo "To: ${MAILTO}" > ${MAILFILE}
echo "X-Priority: 1" >> ${MAILFILE}
echo "Priority: Urgent" >> ${MAILFILE}
echo "Importance: high" >> ${MAILFILE}
echo "X-Message-Flag: Follow up" >> ${MAILFILE}
echo "Subject: ${MAILSUBJECT}" >> ${MAILFILE}
echo -e "Error detected on ${NODE} `date`\n\n" >> ${MAILFILE}
echo -e ${MAILBODY} >> ${MAILFILE}
ls -lth ${FILEDIR}${FMASK} >> ${MAILFILE}
/usr/sbin/sendmail -f ${MAILFROM} -t < ${MAILFILE}
rm ${MAILFILE}
fi
exit ${ERRORLEVEL}
if [ ! -f "/opt/oracle/admin/maintenance/reporting_schedule/status/flag/EDS_report_ready.flag" ]
then
echo "$filename does not exists in the current directory" | mailx -s 'stats' santosh@mail.com
else
echo "$filename does exists in the current directory" | mailx -s 'stats' santosh@mail.com
fi
this script what i written.. i dont know weather its correct or not. Will u please check it once and tel m wat changes hav 2 do..
First, spell out your words...that text-speak junk isn't nice for anyone to try to read.
And if you want to know if your script works or not...why don't you try RUNNING IT and find out???
THEN run your script. It will run your script in debug and show where your error in your script is.
THEN "man test" This will work out for test command criteria.
If i run this command m nt getting any error its running but its not zipping the file.M not getting wer do i change the script so it will gzip the file..
If i run this command m nt getting any error its running but its not zipping the file.M not getting wer do i change the script so it will gzip the file..
I'll ask these AGAIN, since you ignored it the first time:
What output do you get?
Does the find command actually FIND any files to gzip?
As with anything, you need to break all of it down and run each piece by itself. Run the find command by itself first...this is the part where you'll see if it actually FINDS anything TO zip. If so, then you can move to each other piece of the script, one by one, until you find the piece that's broken.
And as you've been told several times, SPELL OUT YOUR WORDS, AND QUIT USING THAT TEXT SPEAK GARBAGE
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.