This script will check, if a drive (like a usb-stick) is attached, and mount it.
Then it "syncs" every 2 seconds to be shure, all data are permanently "up to date" on the stick.
So in theory the stick could be unplugged without unmounting it
Replace "sda9" with the drive-name of your stick.
Save it as "/usr/bin/automountdaemon"
Make it executable:
chmod 755 "/usr/bin/automountdaemon"
Run it:
/usr/bin/automountdaemon &
To stop it, run the second script, "automount-stop".
I did not check it very intensive, and am not shure, if it is really safe to unplug a stick with this method, so see this as "brainstorming", not as a productive solution.
Mark
/usr/bin/automountdaemon
Code: Select all
#!/bin/bash
drive=sda9
oldfreespace=`df /mnt/$drive|grep dev`
freespace=`df /mnt/$drive|grep dev`
echo "automount-daemon started!"
while [ 1 ];do
##############################################################################
## we must sync every 2 seconds to be shure, all data are written to the drive
sync
#######################
## now try to mount it
mount /dev/$drive /mnt/$drive 2>/dev/null
##################################
## if it is mounted, it must have free space,
## so use this to check if mounting had success
oldfreespace=$freespace
freespace=`df /mnt/$drive|grep dev`
##################################
if [ "$freespace" != "$oldfreespace" ];then
if [ "$oldfreespace" == "" ];then
if [ "$freespace" != "" ];then
echo "$drive was mounted"
fi
fi
if [ "$oldfreespace" != "" ];then
if [ "$freespace" == "" ];then
echo "$drive was unplugged"
fi
fi
fi
#####################################
### these lines are only for testing
#if [ "$freespace" != "" ];then
# echo "$drive is mounted"
#else
# echo "$drive is not mounted"
#fi
####################################
#####################################################
## the damon checks if there is a script "automount-stop" running
## In that case it will exit
check=`ps |grep automount-stop|grep -v grep`
if [ "$check" != "" ];then
echo "automount-daemon stopped!"
exit 0
fi
sleep 2
done
Code: Select all
#!/bin/bash
sleep 4