1

This is specific to autofs mounts.

I've found numerous ways to check a traditional mount, some use the /proc/mounts file. I can see my mount is still in that file even when the mount is not currently accessible. i.e. it was accessible, but now device is off or asleep.

These are just some of the methods i tried, which all seem to work for my fstab mounts, but not for my autofs mounts - they simply can't see that the autofs mount is not currently available. Using commands like mount or findmnt seem to hang, and i kill them with CTRL+C.

How to check if a filesystem is mounted with a script

https://stackoverflow.com/questions/9422461/check-if-directory-mounted-with-bash

https://serverfault.com/questions/50585/whats-the-best-way-to-check-if-a-volume-is-mounted-in-a-bash-script

The ultimate goal is to script a check to see if its alive before continuing the script or exiting as appropriate.

This is an example from one of the URLs that always thinks its available, because the mount is in /proc/mounts, even after the device is turned off:

if grep -qs '/mnt/Backups' /proc/mounts; then
  echo "Destination reachable. Continuing..."
else
    echo "Destination unreachable. Exiting."
    exit 1
fi
echo "test done"

A different (better?) example in the URLs was using findmnt. But this doesn't completely work either e.g.

if findmnt /mnt/Backups; then
  echo "Destination reachable. Continuing..."
else
    echo "Destination unreachable. Exiting."
    exit 1
fi
echo "test done"

Findmnt hangs: [UPDATE: I just did some more testing, and this didn't hang this time around - instead it reported as the grep method did - it thought the mount was still alive after i put the server to sleep. But earlier today, it froze]

  • If the share was mounted, but isn't now (e.g. the server shuts down sometime after my desktop had a connection to it).

Works:

  • If the share is mounted - findmnt has no problems (finds mount).
  • If the share hasn't been mounted since i booted my desktop (correctly can't find mount).

I found a RedHat reference. Maybe i need to define a more reliable SOURCE? I'm stuck.

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/s2-sysinfo-filesystems-findmnt

Thanks.

UPDATE: I've also tried just checking for a directory:

https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script

I looked for a directory i knew did not exist on my desktop (smeghead) and it responded as it should - "Destination unreachable. Exiting".

if [ -d "/home/Derek/Desktop/smeghead/" ];  then
    echo "Destination reachable. Continuing..."
else
    echo "Destination unreachable. Exiting."
    exit 1
fi

But if i change the path to /mnt/Backups (when Backups disappears thanks to autofs) then the command freezes and i have to CTRL+C.

So it seems the problem is something about autofs and/or the /mnt location that screws it up?

0

As mentionned in Autofs page on ubuntu.con

Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity

So the first step (after have an autofs configuration which works!-) is to access to the required mountpoint

I use mountpoint like this:

# try to change directory (or ls) to auto-mount if not yet mounted
cd /mnt/Backups/ > /dev/null 2>&1
# check if mounted:
if mountpoint -q /mnt/Backups/; then
    echo "Destination reachable. Continuing..."
else
    echo "Destination unreachable. Exiting."
fi

you can replace cd with ls (or perharps better solution?) if you want to stay in current directory

| improve this answer | |
  • That is a good idea but it did the same i'm afraid. I gave up on using AutoFS and decided to run it over SSH instead. ssh -o ConnectTimeout=15 -o ConnectionAttempts=1 user@ipaddress "cd /path/to/backups/on/server" With the timeout, it doesn't get stuck. – Derek Jan 29 at 23:26
  • @Derek : yes it's very similar as your solution but mountpoint is more dedicated for that and as I have "Destination unreachable", I understood that it required an action (my cd…) to first try to mount it if not already done. – bcag2 Jan 31 at 7:38
  • Thank you for offering an idea (i forgot to say thanks in my previous post, sorry). It worked for me (as my earlier stuff did) if the connection was never there. But if i had an active mount, and then pulled out the network cable - that's when it hangs. Was that your testing scenario? I'm pretty inexperienced with Linux generally, and AutoFS in particular. I have a feeling it's how I have AutoFS configured but it would be interesting to know if the same happens to you in the same circumstances. – Derek Feb 1 at 8:16
  • @Derek: my scenario is I am at a place two days a week and need to save a database from a windows PC. I already set up my autofs on my ubuntu bionic to allow access to this computer when needed. I just create a script to CRON it and retrieve dumps then save it on a correct server. – bcag2 Feb 4 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.