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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-08-2009, 11:51 AM
|
#1
|
Member
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293
Rep:
|
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.
|
|
|
05-08-2009, 12:14 PM
|
#2
|
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora40
Posts: 6,156
|
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.
|
|
|
05-08-2009, 12:30 PM
|
#3
|
Member
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293
Original Poster
Rep:
|
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.
|
|
|
05-08-2009, 01:03 PM
|
#4
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
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.
|
|
|
05-08-2009, 01:14 PM
|
#5
|
LQ 5k Club
Registered: May 2003
Location: London, UK
Distribution: Fedora40
Posts: 6,156
|
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!
|
|
|
05-08-2009, 01:18 PM
|
#6
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
Quote:
Originally Posted by tredegar
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.
|
|
|
05-08-2009, 04:46 PM
|
#7
|
Member
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293
Original Poster
Rep:
|
A problem
Quote:
Originally Posted by i92guboj
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.
|
|
|
05-08-2009, 04:52 PM
|
#8
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
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.
|
|
|
05-08-2009, 10:56 PM
|
#9
|
Member
Registered: Nov 2005
Location: Deerfield MA
Distribution: OpenSuSE, Kubuntu
Posts: 293
Original Poster
Rep:
|
Quote:
Originally Posted by i92guboj
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.
|
|
|
All times are GMT -5. The time now is 12:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|