LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-08-2009, 11:51 AM   #1
pwabrahams
Member
 
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293

Rep: Reputation: 41
Mountedness test for shell scripts


Is there a test that I can use in a shell script to test if a remote mount specified in /etc/fstab has been completed? Better yet, is there a test that will tell me if all mounts have been completed without referring to each of them explicitly?

I'm trying to construct a script that will keep retrying a mount until it is accomplished and then go away.
 
Old 05-08-2009, 12:14 PM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian 11
Posts: 6,133

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
You could test to see if a particular file on the mounted device is present at the mountpoint.
If it's there, the device is mounted. If it's not, it isn't (or someone has deleted the file).

-Or-

mount tells you what is mounted where, and how.

So you could grep the output of mount and test if a particular device is mounted:
Code:
foo=$(mount | grep mountpoint)
if [ "$foo" != "" ] ;  then
echo "Mounted"
else
echo "Not mounted yet"
fi
-Or-

This is linux, so there will be other, possibly more "elegant" ways.
 
Old 05-08-2009, 12:30 PM   #3
pwabrahams
Member
 
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293

Original Poster
Rep: Reputation: 41
The elegant way?

It would be nice if there was something I could put into /etc/rc.local that would keep retrying the mounts until they all succeeded but was independent of the particular contents of /etc/fstab. I suppose I could construct a program that examines /etc/fstab and, for each item mentioning cifs or nfs, tests if it is mounted. But that would be a fair amount of work and might be overkill.

I wish there was a form of the mount command that keeps trying the mount until it succeeds, but there doesn't seem to be, particularly for cifs.
 
Old 05-08-2009, 01:03 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Simple shell scripting will suffice:

Code:
while ! mount /dev/foo /mnt/foo; do sleep 6; done
The loop will only end when mount succeeds. Of course, change that mount command by your own one, and change the 6 seconds period for whatever you wish.
 
Old 05-08-2009, 01:14 PM   #5
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian 11
Posts: 6,133

Rep: Reputation: 419Reputation: 419Reputation: 419Reputation: 419Reputation: 419
Quote:
while ! mount /dev/foo /mnt/foo; do sleep 6; done
I like it
But have no use for it
But my thanks for an "elegant" solution!
 
Old 05-08-2009, 01:18 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by tredegar View Post
I like it
But have no use for it
But my thanks for an "elegant" solution!
Why?

Can't you just put it into any init file? If it locks the boot process just add & to launch it in the background.
 
Old 05-08-2009, 04:46 PM   #7
pwabrahams
Member
 
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293

Original Poster
Rep: Reputation: 41
A problem

Quote:
Originally Posted by i92guboj View Post
Simple shell scripting will suffice:

Code:
while ! mount /dev/foo /mnt/foo; do sleep 6; done
I think there's a problem with this if the mount has succeeded before the script gets executed. In that case you'll get a duplicate mount, which Linux seems to allow.

It also has to be customized for each nfs/cifs entry in fstab and modified whenever the set of remote mounts changes. It's easy to forget to do that when changing fstab. Perhaps an awk program should be used to scan fstab and extract a list of filenames.

Perhaps there just isn't a purely passive way of checking for mountedness.
 
Old 05-08-2009, 04:52 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
The easiest way to see if something's been mounted is to check /etc/mtab.

That's just the basic example on how to do it, of course you can improve it for additional checks, automatic fstab lookup, etc. etc.

The smart way would be to write a service for your distribution that you can add to your runlevel, start or stop at your discretion.
 
Old 05-08-2009, 10:56 PM   #9
pwabrahams
Member
 
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by i92guboj View Post
The easiest way to see if something's been mounted is to check /etc/mtab.
Not the easiest to automate, but the most reliable, I'd agree. I wish there was a variant of the mount command that would produce a condition code for mountedness, but there doesn't seem to be.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
running test scripts with php PhilTR Linux - Server 11 09-12-2006 01:13 PM
LXer: Adodb Multiple Test Scripts Remote Command Execution ... LXer Syndicated Linux News 0 01-09-2006 06:01 PM
scripts - test with if meDream Linux - Newbie 2 05-31-2005 02:13 PM
Shell script to test my shell alltime Programming 5 04-22-2005 07:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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