Check a share is mounted on Linux

Bash script for Linux to check a share is mounted, attempt recovery and remount.

In it’s current form, only outputs anything if action is required. This make it suitable for cron (for my purposes anyway) so that I only get notified if something happened, currently used to make sure a share exists before running a nightly backup.

#!/usr/bin/env sh

# check if share is mounted, attempt recovery and remount

if [ -z "$1" ]; then
    echo "Usage: $0 /mount/point/to/check"
    exit 0

if grep -qs "$DIR" /proc/mounts; then   
    # echo "$DIR is mounted."
    exit 0
    echo "$DIR is not mounted."
    if [ "$(ls -A $DIR)" ]; then
        NEWDIR="$DIR-`date +%F-%k-%M-%S`"
        echo "Files exist so renaming $DIR -> $NEWDIR"
        mv "$DIR" "$NEWDIR"
        mkdir -p "$DIR"
    echo "Attempt remount..."
    mount -a

Also available as a Gist


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.