Revision history for AutomaticDiskDriveMountOnFolderAccess


Revision [21997]

Last edited on 2015-10-07 23:04:10 by ToBo
Additions:
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 [[http://homienaut.com Hominaut]], but you can also use any other solution based on FHEM or other home automation system.
Lets explain the script in few steps
1) I use 'lsof -d /media/usb_drive' to detect if some process is accessing the folder /media/usb_drive. Usually lsof returns nothing.
1) If I access this folder in any way (e.g samba, http, owncloud) then the script is sending a command to Hominaut.
1) Hominaut is sending a radio signal to the plug. The Plug gives power to the hard drive is starting up.
1) The script is mounting the hard drive. It tries again, if something goes wrong.
1) The volume is mounted within seconds after access.
1) When I leave the folder /media/usb_drive then the script tries to unmount the drive. It tries again and again.
1) When umount was successful, then the scripts sends a request to Homienaut to swicth off the hard drive.
The script
Deletions:
We need to check all open files.
%%sudo watch bash -c 'lsof | grep media'%%


Revision [21996]

Edited on 2015-10-07 22:50:46 by ToBo
Additions:
# Version 1
# Written by Andreas Tobola, October 2015, http://tnotes.de
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
Deletions:
# Written by Andreas Tobola, Oktober 2015, http://tnotes.de
FOLDER='/home/actor/media/black'
OPEN_FILES=`lsof +d $FOLDER -b -t | wc -l`
if [ "$OPEN_FILES" -gt "0" ]; then
echo 'Folder open'
else
echo 'Folder closed'
fi
echo 'end'


Revision [21992]

Edited on 2015-10-07 21:33:03 by ToBo
Additions:
=====Automatic Disk Drive Mount On Folder Access=====
%%(bash)
Deletions:
=====Disk Drive Mount On Folder Access=====


Revision [21991]

Edited on 2015-10-07 21:32:48 by ToBo
Additions:
=====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
# Written by Andreas Tobola, Oktober 2015, http://tnotes.de
# Scrip must be run as root to access all open files by any process
FOLDER='/home/actor/media/black'
OPEN_FILES=`lsof +d $FOLDER -b -t | wc -l`
if [ "$OPEN_FILES" -gt "0" ]; then
echo 'Folder open'
else
echo 'Folder closed'
fi
echo 'end'
%%
Deletions:
=====T=====


Revision [21990]

The oldest known version of this page was created on 2015-10-07 21:14:33 by ToBo
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki