Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm mounting a truecrypt container via the command line (i.e. truecrypt file dir). Is there any way to have truecrypt automatically unmount the container after a given amount of inactivity in dir?

share|improve this question
That's a job for an automounter. Last time I used one on Linux, they kind of sucked, but I think they've improved in the last decade. – Gilles Sep 5 '12 at 22:25
@Gilles Thanks for pointing it out, I'll try and find one for this – Tobias Kienzler Sep 6 '12 at 6:04
Can truecrypt be mounted with the mount command? – Nils Sep 6 '12 at 21:24
Could you perhaps use something similar to @ultrasawblade's script and leverage lsof (or possibly /proc/*/fd or somesuch; lsof gets its data from somewhere...)? It wouldn't be perfect (very fast accesses would go uncaught, and it would cause a bit of CPU hammering), but I'm guessing it's either something like that, or access notifies (which at the very least means write a system application, not a script). – Michael Kjörling Jan 4 '13 at 12:40

Truecrypt can't do this but something like the following would accomplish what you are trying to do:


cd /
echo "$0: Proceeding with dismount in 5 minutes." > /dev/console
# or something else to notify user that the volume will be unmounted
sleep 4.5m
echo "$0: Proceeding with dismount in 30 seconds." > /dev/console
sleep 30s
sync; truecrypt $@

The cd / would be to ensure that the current directory is never within the mounted truecrypt volume, just in case you invoked it under different circumstances.

Save this somewhere, chmod +x it, and call it something like truecrypt-auto-dismount, and then

truecrypt {mount-options}; truecrypt-auto-dismount {dismount-options}

I would definitely incorporate some type of notification so you have a chance to close open files. Of course you could get rather elaborate and try to incorporate a loop that kills with SIGTERM (and then does SIGKILL if it doesn't respond in a timeframe) all processes listed by lsof | grep /mnt/your-truecrypt-volume if you wanted.

share|improve this answer
Thanks, that's a general timeout, but I'm looking for a possiblity to have the timer reset whenever the mounted dir is accessed again. And truecrypt.org/docs/?s=background-task suggest that this is possible when using the GUI (which I can't) – Tobias Kienzler Sep 5 '12 at 14:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.