Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Bash script to check if smbfs share is mounted

Posted on 2003-03-07
5
Medium Priority
?
293 Views
Last Modified: 2010-04-20
I have a linux web server (apache) that on boot (via fstab) attempts to mount a remote share (an mp3 directory) to a mount point in the wwwroot (so that i can access it with http://server/mp3s/).  The problem is, if the machine with the mp3s is off, I'd like it to try to reconnect every 15 minutes or so.  I can implement this as a cron job, but don't know how I'd write a script that does this.  Could someone write me a quick script that:
1. checks if //mp3comp/music is mounted
2. if not attempts to mount it
3. does not send errors connecting (if mp3comp off) to stderr or stdout, nor log them (just discard to /dev/null)
If it turns out to be a harder task then I think, that I'll raise the point reward accordingly.

Much appreciated.

-Terence

Oh btw, If anyone has a better idea to solve my problem than I've proposed, I'm open to suggestions. I guess I could have mp3comp send some sort of initialization packet to the server when it boots, but I think that'd be much to complicated.  Open to suggestions though.
0
Comment
Question by:tvh2k
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
Gns earned 90 total points
ID: 8093421
Untested and with a need to be adjusted to your specific setup:

#!/bin/sh
a=`mount | grep "//mp3comp/music"`
if [ "$a" != "" ]
then
    mount /mount/point/for/the/mp3box
fi
# EOF

-- Glenn
0
 
LVL 20

Expert Comment

by:Gns
ID: 8093426
You might need to set PATH explicitly too
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

just after the hash-bang line (#!/bin/sh).

-- Glenn
0
 

Author Comment

by:tvh2k
ID: 8229357
Thanks Glenn...I'll try this out over the weekend and accept your answer if it works.

-Terence
0
 

Author Comment

by:tvh2k
ID: 8234386
Well, thanks for getting me started on the right foot.  Two errors with your script: it should be if $a == "" (because if grep could not find that name in the mount list I want it mounted) and I wanted "&> /dev/null" at the end of the mount (stdout and stderr to null), but I could figure those out my self.  Thanks for the help, accepted as answer.

By the way, since I know e-e shows up a lot of google, here's what the script that I am using looks like (for those who are looking for a similar solution):

#!/bin/sh
mounted=`/bin/mount | grep "//mp3comp/music"
if ["$mounted" == ""]
then
   #this mount point entry is in fstab
   /bin/mount /mnt/music &> /dev/null
fi


I then added this to my /etc/crontab:

0-59/20 * * * * root /path/scriptname

So that it will attempt to connect every 20 minutes.
Thanks again Glenn.
0
 
LVL 20

Expert Comment

by:Gns
ID: 8235205
I told you it was untested... hopefully one would find those "problem areas" during initial testing;-).

A bit sloppy of me to miss the /dev/null bit... OTOH it was produced in the time it took to read your initialö question:-).

Happy Linuxing
-- Glenn
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question