LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script For sending mail if disk is not mounted (https://www.linuxquestions.org/questions/linux-newbie-8/script-for-sending-mail-if-disk-is-not-mounted-918682/)

Vi_rod 12-14-2011 06:41 AM

Script For sending mail if disk is not mounted
 
Hello,
I'm just starting out, Please help in writing a script for -

mount | grep "ip"
if [mounted ]
then echo - it is mounted >> somefile.txt
else
send mail - not mounted

zQUEz 12-14-2011 06:54 AM

To literally answer your question, how about:
Code:

if [ `mount |grep IP |wc -l` -eq 1 ];
then echo "it is mounted" >> somefile.txt;
else
echo "not mounted" |mail -s "not mounted" to.someone@who.cares.com;
fi

?

The only thing you might be concerned with is if that "grep" will still return that IP even if that mount is not truly accessible. For example, mount might show it mounted but in an error state. So you would be wise to add a further test to see if it is actually accessible by touching a file on the remote end (assuming you have write access) or just seeing if a known file exists. (like `if [ -d /known/file/at/remote/end]...).

Vi_rod 12-14-2011 07:51 AM

Cool! thanks...
bad i couldnt crack this :):redface:

asimba 12-14-2011 08:13 AM

Code:

[[  -z  `mount | grep -i ip` ]] &&  echo " Not Mounted" || echo "Mounted"
what mailing software you use ? mutt /mail ?

Vi_rod 12-14-2011 08:24 AM

we use mail , dint even know about mutt

asimba 12-14-2011 08:28 AM

Could you possibly post a command - how you send a usual mail / normal mail.


As for above "code" create a file test.sh
mark it executable
and paste that single line
execute script with "ip" mounted and discounted - and verify results.

Cedrik 12-14-2011 08:31 AM

Or just grep /proc/mounts
Code:


if grep -i "ip" /proc/mounts > /dev/null 2>&1; then
  echo "it is mounted" >> somefile.txt
else
  echo "not mounted" | mail -s "not mounted" "someone@host"
fi


Vi_rod 12-14-2011 09:01 AM

Hi,
I use this syntax -> mail -s "test" someone@somwhere.com

Vi_rod 12-14-2011 09:22 AM

This "/dev/null 2>&1;" part is confusing. could find some help on google - I could only understand that output is going to a null file. so does it mean it gets overwritten if i use this in multiple scripts running on Cron at same time? would this lead to an error??

Cedrik 12-14-2011 10:36 AM

Quote:

Originally Posted by Vi_rod (Post 4549731)
This "/dev/null 2>&1;" part is confusing. could find some help on google - I could only understand that output is going to a null file. so does it mean it gets overwritten if i use this in multiple scripts running on Cron at same time? would this lead to an error??

/dev/null is your OS black hole, it isn't overwritten, it absorbs everything you send to it :p

2>&1 is a bash expression, it means send error output (#2) to standard output (#1)

So sending output > /dev/null 2>&1 means suppress both standard and error output

asimba 12-14-2011 11:45 AM

This single line should do the trick

create a empty file - copy this code - chmod +x - You are all set

Code:

[[  -z  `mount | grep -i ip` ]] &&  mail -s "ip not mounted" someone@somwhere.com

Cedrik 12-14-2011 12:14 PM

Nice but does not fit OP requirement (when ip is mounted, write line to a text file)

asimba 12-14-2011 12:21 PM

Quote:

Originally Posted by Cedrik (Post 4549881)
Nice but does not fit OP requirement (when ip is mounted, write line to a text file)

Following should redirect to txt file if ip is mounted
Code:

[[  -z  `mount | grep -i ip` ]] &&  mail -s "ip not mounted" someone@somwhere.com || echo "ip is mounted"

Cedrik 12-14-2011 12:25 PM

I have a question with this type of script line

Say the ip is not mounted, then it does an attempt to send mail, but if for some reason mail command fails,
will it echo "ip is mounted" ?

asimba 12-14-2011 12:27 PM

Quote:

Originally Posted by Cedrik (Post 4549890)
I have a question with this type of script line

Say the ip is not mounted, then it does an attempt to send mail, but if for some reason mail command fails,
will it echo "ip is mounted" ?

Well noticed!

----

Correct would have been this one
Code:

[[ ! -z  `mount | grep -i ip` ]] &&  echo "ip is mounted" || mail -s "ip not mounted" someone@somwhere.com


All times are GMT -5. The time now is 12:24 AM.