LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-14-2011, 06:41 AM   #1
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Rep: Reputation: Disabled
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
 
Old 12-14-2011, 06:54 AM   #2
zQUEz
Member
 
Registered: Jun 2007
Distribution: Fedora, RHEL, Centos
Posts: 294

Rep: Reputation: 54
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]...).

Last edited by zQUEz; 12-14-2011 at 06:58 AM.
 
1 members found this post helpful.
Old 12-14-2011, 07:51 AM   #3
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
Cool! thanks...
bad i couldnt crack this
 
Old 12-14-2011, 08:13 AM   #4
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

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

Last edited by asimba; 12-14-2011 at 08:18 AM. Reason: added code tags
 
1 members found this post helpful.
Old 12-14-2011, 08:24 AM   #5
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
we use mail , dint even know about mutt
 
Old 12-14-2011, 08:28 AM   #6
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
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.
 
Old 12-14-2011, 08:31 AM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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

Last edited by Cedrik; 12-14-2011 at 08:33 AM.
 
1 members found this post helpful.
Old 12-14-2011, 09:01 AM   #8
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
Hi,
I use this syntax -> mail -s "test" someone@somwhere.com
 
Old 12-14-2011, 09:22 AM   #9
Vi_rod
Member
 
Registered: Dec 2011
Posts: 42

Original Poster
Rep: Reputation: Disabled
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??
 
Old 12-14-2011, 10:36 AM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Vi_rod View Post
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

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

Last edited by Cedrik; 12-14-2011 at 10:38 AM.
 
1 members found this post helpful.
Old 12-14-2011, 11:45 AM   #11
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
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
 
Old 12-14-2011, 12:14 PM   #12
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Nice but does not fit OP requirement (when ip is mounted, write line to a text file)
 
Old 12-14-2011, 12:21 PM   #13
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
Quote:
Originally Posted by Cedrik View Post
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"
 
Old 12-14-2011, 12:25 PM   #14
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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" ?
 
1 members found this post helpful.
Old 12-14-2011, 12:27 PM   #15
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
Quote:
Originally Posted by Cedrik View Post
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

Last edited by asimba; 12-14-2011 at 12:32 PM. Reason: error
 
1 members found this post helpful.
  


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
Sending mail from shell script sinoob Linux - Server 3 07-07-2011 07:43 AM
problem with sending mail from script vedaprasad Programming 6 03-18-2011 09:31 PM
Sending mail with subject in Linux script 151803 Linux - General 5 01-25-2007 04:46 AM
Shell script sending mail mfrick Linux - General 5 09-01-2005 07:46 PM
How to Do the Following? Running Script at Boot-Time + Sending Mail LinuxGeek Linux - General 1 07-20-2004 12:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:17 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