Automatic Disk Drive Mount On Folder Access
This script is switching the power of an external hard drive on demand and is mounting the drive automatically. It's also unmounting the drive if not accessed any more and is switching the power off.
I use a radio controlled plug to switch a USB hard drive on and off. To control the hard drive the script is sending a command to Hominaut, but you can also use any other solution based on FHEM or other home automation system.
Lets explain the script in few steps
- I use 'lsof -d /media/usb_drive' to detect if some process is accessing the folder /media/usb_drive. Usually lsof returns nothing.
- If I access this folder in any way (e.g samba, http, owncloud) then the script is sending a command to Hominaut.
- Hominaut is sending a radio signal to the plug. The Plug gives power to the hard drive is starting up.
- The script is mounting the hard drive. It tries again, if something goes wrong.
- The volume is mounted within seconds after access.
- When I leave the folder /media/usb_drive then the script tries to unmount the drive. It tries again and again.
- When umount was successful, then the scripts sends a request to Homienaut to swicth off the hard drive.
The script
#!/bin/bash
# Automatic Disk Drive Mount On Folder Access
# Version 1
# Written by Andreas Tobola, October 2015, http://tnotes.de
# Scrip must be run as root to access all open files by any process
FOLDER='/media/usb_drive'
# start condition
LAST_STATE=0
umount $FOLDER
while [ true ]
do
OPEN_FILES=`lsof +d $FOLDER -b -t | wc -l`
if [ "$OPEN_FILES" -gt "0" ]; then
# Folder is open. Mount volume if not mounted yet.
if [ "$LAST_STATE" -eq "0" ]; then
echo Folder $FOLDER has been accessed
curl 'http://homienaut.fritz.box:8084/fhem?cmd.SilverHD=set%20SilverHD%20on&room=Server'
if [ "$?" -eq "0" ]; then
echo Volume power on request sent
sleep 4 # Give the hard drive x seconds to get ready
mount $FOLDER
if [ "$?" -eq "0" ]; then
# Check if volume is mounted and then wait otherwise try again
echo Volume mounted
LAST_STATE=1
sleep 10
fi
fi
fi
else
if [ "$LAST_STATE" -eq "1" ]; then
echo Folder $FOLDER is not accessed by any process anymore
sync
umount $FOLDER
if [ "$?" -eq "0" ]; then
echo Volume umounted
curl 'http://homienaut.fritz.box:8084/fhem?cmd.SilverHD=set%20SilverHD%20off&room=Server'
echo Volume switched off
LAST_STATE=0
sleep 10
fi
fi
fi
sleep 1
done
# Automatic Disk Drive Mount On Folder Access
# Version 1
# Written by Andreas Tobola, October 2015, http://tnotes.de
# Scrip must be run as root to access all open files by any process
FOLDER='/media/usb_drive'
# start condition
LAST_STATE=0
umount $FOLDER
while [ true ]
do
OPEN_FILES=`lsof +d $FOLDER -b -t | wc -l`
if [ "$OPEN_FILES" -gt "0" ]; then
# Folder is open. Mount volume if not mounted yet.
if [ "$LAST_STATE" -eq "0" ]; then
echo Folder $FOLDER has been accessed
curl 'http://homienaut.fritz.box:8084/fhem?cmd.SilverHD=set%20SilverHD%20on&room=Server'
if [ "$?" -eq "0" ]; then
echo Volume power on request sent
sleep 4 # Give the hard drive x seconds to get ready
mount $FOLDER
if [ "$?" -eq "0" ]; then
# Check if volume is mounted and then wait otherwise try again
echo Volume mounted
LAST_STATE=1
sleep 10
fi
fi
fi
else
if [ "$LAST_STATE" -eq "1" ]; then
echo Folder $FOLDER is not accessed by any process anymore
sync
umount $FOLDER
if [ "$?" -eq "0" ]; then
echo Volume umounted
curl 'http://homienaut.fritz.box:8084/fhem?cmd.SilverHD=set%20SilverHD%20off&room=Server'
echo Volume switched off
LAST_STATE=0
sleep 10
fi
fi
fi
sleep 1
done
Siehe auch