Bash script to check if smbfs share is mounted

Posted on 2003-03-07
Medium Priority
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.


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.
Question by:tvh2k
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 20

Accepted Solution

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

a=`mount | grep "//mp3comp/music"`
if [ "$a" != "" ]
    mount /mount/point/for/the/mp3box

-- Glenn
LVL 20

Expert Comment

ID: 8093426
You might need to set PATH explicitly too
export PATH

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

-- Glenn

Author Comment

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


Author Comment

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):

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

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.
LVL 20

Expert Comment

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

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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