This script unmounts the filesystem and removes the mapper if the filesystem is not in use.
You can have cron run it every 10 minutes or so if you don't want your encrypted fs to be mounted all the time.
Change $mappername and $mpoint to match your setup.
#!/bin/sh
mappername="secure"
mpoint="/mnt/secure"
if grep $mpoint /etc/mtab > /dev/null; then
fuser -m $mpoint > /dev/null || (umount /mnt/secure/; cryptsetup remove $mappername)
else
echo $mpoint is not mounted!
if [ -e /dev/mapper/$mappername ]]; then
echo "Removing the mapper " $mappername
cryptsetup remove $mappername
fi
fi