Hey,
I just wanted to add that I wrote a similar script that I use on my laptop, I posted it on an other forum some while ago.
Link
The idea is to use a laptop because it is battery powered. My script tests every 5 seconds if the laptop is running on battery, if this is the case it releases immediately. My idea was that a power failure is unexpected and not a good thing, not to mention getting stuck if the battery runs out before the timer does
Code: Select all
!/bin/bash
DRIVE='/dev/sr0' #Change this to the location of your drive
read -p "How many minutes before release? : " TIME
echo "Starting timer: $TIME minutes"
TIMESEC=`expr $TIME \* 60`
NLOOPS=`expr $TIMESEC / 5` #loop every 5 seconds
i=0
while [ $i -le $NLOOPS ]
do
#check if we still have mains power
STATUS=`acpi | grep Discharging | wc -l`
if test $STATUS -gt 0
then
echo "Laptop is running on battery, releasing now..."
eject -T $DRIVE
exit 1
fi
#wait 5 secs
sleep 5;
i=$(( i+1 ))
echo "`expr $TIMESEC - 5 \* $i` seconds to go..."
done
echo "Time is up, releasing..."
eject -T $DRIVE
exit 0
Of course test it before using it, as mentioned by LoKiT you must change /dev/sr0 in te script to whatever drive you want to release.
I only use this for short sessions (15 minutes or so) but I did not build in any limit, check what you have typed before hitting enter.