2

I need to insert on some service scripts a condition on the top to check whether a device is mounted or not.

I'm not very familiar with bash script...

This is a first try I wrote but it doesn't work.

### START CHECK
volume="/media/MyMountName"
if ! mount | grep "on ${volume} type" > /dev/null
then
    exit;
fi
### END CHECK

#... rest of the service script

This is another solution I'd like best:

### START CHECK
volume="/media/MyMountName"
delay=5

while ! mount | grep "on ${volume} type" > /dev/null
do
    sleep $delay
    if delay >= 60
    then
        exit;
    $delay = $dealy + 5
done
### END CHECK

#... rest of the service script

The second one should try to check the mount for a minute before giving up and exit without running the service.

3 Answers 3

1
while ! mount | grep "on ${volume} type" > /dev/null; do
    sleep $delay
    if [ "$delay" -gt 60 ]; then
        exit
    fi
    delay=$((delay+5))
done

using /proc/mounts

You might consider using /proc/mounts instead of the output of mount (which is just /etc/mtab).

while ! grep " ${volume} " /proc/mounts &>/dev/null; do
1
  • Ok... I see some syntax errors... :-) Thanks! Can you please add another code example to show me how should I check the same condition using /proc/mounts as you suggest? Feb 4, 2016 at 11:40
0

You're pretty close. How about:

### START CHECK
start_check_mtpt() { 
 local volume="$1"
 local delay=5
 local tries=$[ 60 / delay ]
 local mounted=0

 while [[ 0 = $mounted ]] && [[ $tries -ge 0 ]]; do
   if cut -d' ' -f2 /etc/mtab | grep -qF "${volume}" ; then
      mounted=1
      # optional: break
   else
    sleep $delay
    let tries=tries-1
   fi
 done
 [[ 1 = $mounted ]]
 return $?
}
### END CHECK

start_check "/media/MyMountName"
1
  • Or just use /proc/mounts instead of /etc/mtab
    – Otheus
    Feb 4, 2016 at 11:53
0

There are several answers to this question. If you want to check, whether a specifc device is mounted or not (i.e. your backup device), then you should check it by its UUID, which you can find out by issuing blkid.

UUID="place the UUID here"
TRIES=0
DEVFILE=""

while [[ -z $DEVFILE ]] && [[ $TRIES -lt 5 ]]; do
   DEVFILE=$(blkid -U $UUID)
   TRIES=$(( $TRIES + 1 ))
   sleep 5
done

if [ $TRIES -lt 5 ]; then
   MOUNTPOINT=$(findmnt -f $DEVFILE | tail -n1 | cut -d" " -f1)
   echo "found your device at $MOUNTPOINT"
fi
2
  • I have no UUID for my device, probably because it's a NFS mount. When I run sudo blkid I get 3 /dev/... but not my mount that should be /media/MyMountName. Feb 4, 2016 at 13:50
  • Yes. When you issue blkid, then you should get a list of the devices that are currently plugged into your system (but not necessarily mounted). Blkid gives you the device file name and the UUID but not the mountpoint. When you have the device filename (namely $DEVFILE) you can check out the mountpoint by using findmnt -f $DEVFILE. If the device is not mounted, then findmnt gives you an empty string. Feb 4, 2016 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .