Revision [21996]
This is an old revision of AutomaticDiskDriveMountOnFolderAccess made by ToBo on 2015-10-07 22:50:46.
Automatic Disk Drive Mount On Folder Access
We need to check all open files.
sudo watch bash -c 'lsof | grep media'
#!/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