What's the best way to check if a volume is mounted in a Bash script?
What I'd really like is a method that I can use like this:
if <something is mounted at /mnt/foo>
then
<Do some stuff>
else
<Do some different stuff>
fi
|
|
Avoid using Avoid piping Simply:
|
|||||||||||||||||||||
|
or
|
|||||
|
|
the following is what i use in one of my rsync backup cron-jobs. it checks to see if /backup is mounted, and tries to mount it if it isn't (it may fail because the drive is in a hot-swap bay and may not even be present in the system) NOTE: the following only works on linux, because it greps /proc/mounts - a more portable version would run 'mount | grep /backup', as in Matthew's answer..
if ! grep -q /backup /proc/mounts ; then
if ! mount /backup ; then
echo "failed"
exit 1
fi
fi
echo "suceeded."
# do stuff here
|
|||
|
|
|
A script like this isn't ever going to be portable. A dirty secret in unix is that only the kernel knows what filesystems are where, and short of things like /proc (not portable) it'll never give you a straight answer. I typically use df to discover what the mount-point of a subdirectory is, and what filesystem it is in. For instance (requires posix shell like ash / AT&T ksh / bash / etc)
Kinda tells you useful information. |
|||
|
|
|
Since in order to mount, you need to have a directory there anyway, that gets mounted over, my strategy was always to create a bogus file with a strange filename that would never be used, and just check for it's existence. If the file was there, then nothing was mounted on that spot... I don't think this works for mounting network drives or things like that. I used it for flash drives. |
|||||
|
|
|
None of these satisfy the use case where a given directory is a sub directory within another mount point. For example, you might have /thing which is an NFS mount to host:/real_thing. Using grep for this purpose on /proc/mounts /etc/mtab or 'mount' will not work, because you will be looking for a mount point that doesn't exist. For example, /thing/thingy is not a mount point, but /thing is mounted on host:/real_thing. The best answer voted on here is actually NOT "the best way to determine if a directory/volumne is mounted". I'd vote in favour using 'df -P' (-P POSIX standards mode) as a cleaner strategy:
The output from running this will be:
If you want to know what the real mount point is, no problem:
The output from that command will be:
This is all very useful if you are trying to create some sort of chroot that mirrors mount points outside of the chroot, within the chroot, via some arbitrary directory or file list. |
|||
|
|
|
Does it need to be any more complicated than:
? |
||||
|
Although this is a Linux question, why not make it portable when it is easily done? The manual page of grep says: So I propose the following solution:
|
|||
|
|
|
grep /etc/mtab for your mount point maybe? |
|||
|
This?:
From: An Ubuntu forum |
|||
|
|
|
How about comparing devices numbers? I was just trying to think of the most esoteric way..
There a flaw in my logic with that ... As a Function:
The echo error messages are probably redundant, because stat will display the an error as well. |
||||
|
|
Sorry for bringing this up but I think this is pretty usefull:
This gets the 2nd column of /proc/mounts (2nd column = mount points). Then it greps the output. Note the ^ and $, this prevents /backup from matching /mnt/backup or /backup-old etc. |
|||
|
|