Mery84 has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
I need help with perform test on mount points.
I need to be sure that $usbdev is mounted on $usbmount if not - do some bunch of code like write warning to logfile and mail me.
$usbdev = "/dev/sde1/"; $usbmount = "/media/usb/"; system("mount $usbdev $usbmount"); Here should be test
When I do mount how to check is this operation is successful and files copied to $usbmount are copied to usb stick? If $usbdev isn`t mounted the copy part of script stil works, files are stored on / partition where /media dir is placed.
Simple check:
if (! -e "$usbmount") { print "error \n"; }
fails - error is returned always no matter is usb stick mounted or not, is it pluged in or not.

Replies are listed 'Best First'.
Re: How to verify mount point
by Eliya (Vicar) on Mar 23, 2012 at 15:57 UTC

    Maybe you want to check the output of the mount command (called without args), or the contents of /proc/mounts, if you have a system with a proc file system.

Re: How to verify mount point
by Tux (Canon) on Mar 23, 2012 at 16:03 UTC
Re: How to verify mount point
by moritz (Cardinal) on Mar 23, 2012 at 16:07 UTC
Re: How to verify mount point
by JavaFan (Canon) on Mar 23, 2012 at 16:24 UTC
    Assuming you aren't using the program as root, one way of doing it is to give /media/usb permission 755. Any attempt to write to the directory will fail if the device hasn't been mounted. You'll have to add user to the list of options in /etc/fstab for the device.

    Otherwise, you can just parse the output of mount, or look in /proc/mounts or in /etc/mtab.

Re: How to verify mount point
by reisinge (Hermit) on Mar 23, 2012 at 19:02 UTC
    You could use unless control structure to execute the code in the block just upon successful mount:
    use strict; use warnings; use File::Copy; my $usbdev = "/dev/sdb1/"; my $usbmount = "/media/usb/"; my @files = qw( file1 file2 file3 ); # or read from the filesystem usi +ng a glob or directory handle unless ( system "mount $usbdev $usbmount" ) { # Return was zero - meaning success for my $file (@files) { copy($file,$usbmount) or warn "Failed to copy $file: $!\n"; } ! system "umount $usbmount" or die "Can't umount $usbmount\n"; }

    Have a nice day, j

      Many thanks for this example - this is short, simple and works excellent.
      Thanks to all of you Monks for your support :).
Re: How to verify mount point
by salva (Canon) on Mar 23, 2012 at 22:51 UTC