LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-15-2012, 05:44 AM   #1
santosh662
LQ Newbie
 
Registered: Feb 2012
Posts: 10

Rep: Reputation: Disabled
Need advice on writing a script to check if a file exists and send notification


HI,

I am new to it

i need script that weather the file received or not. through mail confirmation
If any one knows the script please post ASAP
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-15-2012, 05:47 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Sounds like a homework exercise to me.
 
Old 02-15-2012, 06:03 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Hi and welcome to LinuxQuestions!
Quote:
Originally Posted by santosh662 View Post
If any one knows the script please post ASAP
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.

Thank you in advance for your collaboration.
 
2 members found this post helpful.
Old 02-16-2012, 05:57 AM   #4
santosh662
LQ Newbie
 
Registered: Feb 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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..
 
Old 02-16-2012, 06:14 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 02-16-2012, 06:25 AM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by santosh662 View Post
Will u please check it once and tel m wat changes hav 2 do..
Well,
If you want/ need help regarding your question then change your attitude first.

Read LQ rules and learn how to write and speak English, so SPELL out your words at least.

good luck!

Last edited by lithos; 02-16-2012 at 06:28 AM.
 
Old 02-16-2012, 06:57 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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}
 
Old 02-16-2012, 08:28 AM   #8
santosh662
LQ Newbie
 
Registered: Feb 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
thank you so much of your help
@colucix- i will write by using th if/else then statement any will try it once..
 
Old 02-16-2012, 10:31 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by santosh662 View Post
Code:
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???
 
Old 02-16-2012, 11:26 AM   #10
pafoo
Member
 
Registered: Jul 2011
Location: Alabama
Distribution: Red Hat/Ubuntu/Solaris
Posts: 37

Rep: Reputation: 11
Here is a bash tip.

#!/bin/bash -x

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.
 
Old 02-22-2012, 12:01 AM   #11
santosh662
LQ Newbie
 
Registered: Feb 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
find /dev/mapper/vg0-vfuservol -name "*.txt" -o -name "*.csv.gpg" -o -name "*.txt.gpg" -o -name "*.met.gpg" -o -name "*.dat" -o -name "*.dat.gpg" -mtime +1 -print -exec gzip {} \;

hi friends

the above script is for gzipping the files.
its running but not gizzipng the files
please any one help us wer i need 2 change in script

Thanks in adavance
 
Old 02-23-2012, 08:56 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by santosh662 View Post
find /dev/mapper/vg0-vfuservol -name "*.txt" -o -name "*.csv.gpg" -o -name "*.txt.gpg" -o -name "*.met.gpg" -o -name "*.dat" -o -name "*.dat.gpg" -mtime +1 -print -exec gzip {} \;

hi friends

the above script is for gzipping the files. its running but not gizzipng the files
please any one help us wer i need 2 change in script

Thanks in adavance
Again, you need to spell out your words.

And what happens if you type that in on the command line? What output do you get? Any error(s)? Does the find command actually FIND any files to gzip?
 
Old 02-24-2012, 02:36 AM   #13
santosh662
LQ Newbie
 
Registered: Feb 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi,


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..
 
Old 02-24-2012, 06:05 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Again, you need to spell out your words. ...
 
Old 02-24-2012, 09:25 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by santosh662 View Post
Hi,


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
 
  


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 File Exists shady4u Linux - General 2 02-21-2010 03:05 AM
Need a script which does Health check for Linux and send mail notification? your_shadow03 Linux - Newbie 6 11-26-2009 06:20 PM
using if in bash script to check if prog exists karuna-bdc Linux - Newbie 6 06-21-2009 07:23 PM
check if directory exists using shell script v333k Programming 9 04-23-2009 09:29 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:22 PM.

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