Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
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.
I want to be able to mount, umount, then verify the mount directory is no longer mounted, before deleting it (rmdir), all via script. How can I verify that it's no longer mounted so I don't accidentally blow away the data it was mounted to?
rmdir by itself deletes only empty directories (otherwise returns an error message). From a script you can verify a mounted/unmounted device by the mount command (without any option), e.g. by something like
Code:
if [ `mount | grep /dev/fd0 | wc -l` -eq 0 ] then
rmdir /mnt/floppy
fi
assuming /dev/fd0 the device (floppy) to be unmounted and /mnt/floppy the mount point. You can also use a while loop to check until the device is truly unmounted then execute the rmdir command.
Perhaps you should be looking at the automount system and udev to automate mounts for various devices. Typically, automount automatically mounts things when they're referenced, and umounts them after they have been unused for a specified period of time. (Ten minutes by default, if I remember correctly.) Then your script would have no need to "clean up" after itself.
I'm also not understanding why an empty directory needs to be "cleaned up"...
Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)
I'm also not understanding why an empty directory needs to be "cleaned up"...
Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)
The script is a backup script that creates 2 directories for each server, then mounts a source and destination Windows share to each, respectively. Files are transferred across, then I umount the directories. After that I want to remove the directories. I will have multiple scripts moving across multiple servers and destinations, and I don't want the clutter of the temporary directories. To me it's the same as leaving a candy wrapper on the table after you've finished the candy.
Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)
Thank you for pointing out, anomie. Indeed, my suggestion implies you already know which is the device being umounted (on the other hand in the OP situation a previous umount command has been issued). So - litteraly - if you not find it in the list of the mounted devices (that is the umount has been accomplished), then you can safely remove the mount pount (not the device itself). Furthermore, using the rmdir command ensures the directory is empty before trying to remove.
BTW the solution proposed by iamnothere is far more elegant! Why I never think at &&...!?!
Thanks for all the info. I didn't realize rmdir only removes empty directories by default.
P.S. to iamnothere: The big deal is that while I can mount the source as read-only, I must mount the destination as writable. If for some reason the umount failed and my script issued rmdir or rm -rf on the destination directory, my destination data would be murdered. Had it happen once. Don't want to do it again.
Thanks for all the info. I didn't realize rmdir only removes empty directories by default.
P.S. to iamnothere: The big deal is that while I can mount the source as read-only, I must mount the destination as writable. If for some reason the umount failed and my script issued rmdir or rm -rf on the destination directory, my destination data would be murdered. Had it happen once. Don't want to do it again.
But, as others pointed out and you now realize, rmdir only removes empty directories (not just by default, it _cannot_ remove non-empty directories). You can rmdir / as root with no ill effects, since nothing will happen. Obviously, rm -fr is a different story, and needs to be used with care.
Another solution to ensure a mount point is unmounted is to stat it, then stat the parent directory. stat returns the device number (major/minor), so if the're different, the directory has a file system mounted on it, if the're the same, then clearly not.
e.g.
Quote:
if [[ $(stat -c %d /mnt) == $(stat -c %d /mnt/point) ]]; then
# ... fs is unmounted
else
# umount failed, try again...
fi
Last edited by iamnothere; 03-02-2007 at 10:32 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.