ABTME

My own website

Script to see if NFS is mounted

Byandrewt-admin

Jul 16, 2020 #bash, #linux

#!/usr/bin/env bash

MP=’/mnt/remote’
HOSTNAME=hostname -s
FILE=/tmp/is_it_mounted.txt

if grep -qs $MP /proc/mounts; then
echo “OK $MP mounted on $HOSTNAME” > /dev/null
else
echo “NOK $MP not mounted on $HOSTNAME” > $FILE
fi

if [ -f ${FILE} ]
then
/usr/sbin/sendmail -t << EOF
From: ${HOSTNAME}
To: myemail@email.local
Subject: ${HOSTNAME} – NFS mount not mounted – Please Investigate
cat ${FILE}
EOF

#Clean up output file

rm ${FILE}

fi

Another example

Bash functions:

#These functions return exit codes: 0 = found, 1 = not found

isMounted    () { findmnt -rno SOURCE,TARGET "$1" >/dev/null;} #path or device
isDevMounted () { findmnt -rno SOURCE        "$1" >/dev/null;} #device only
isPathMounted() { findmnt -rno        TARGET "$1" >/dev/null;} #path   only

#where: -r = --raw, -n = --noheadings, -o = --output

Usage examples:

if isPathMounted "/mnt/foo bar";      #Spaces in path names are ok.
   then echo "path is mounted"
   else echo "path is not mounted"
fi

if isDevMounted "/dev/sdb4"; 
   then echo "device is mounted"
   else echo "device is not mounted"
fi

#Universal:
if isMounted "/mnt/foo bar"; 
   then echo "device is mounted"
   else echo "device is not mounted"
fi

if isMounted "/dev/sdb4";
   then echo "device is mounted"
   else echo "device is not mounted"
fi