title: flock(1) is failing to release lock
body: <p>I have a set of shell scripts that perform backups on my system using truecrypt and rsync. I want to avoid any possibility of these various scripts running at the same time.</p>
  
  <p>They use <code>flock</code> in a subshell, as described in the manpage:</p>
  
  <pre><code>(
      flock -n 9 || exit 1
      # ... commands executed under lock ...
  ) 9&gt;/var/lock/mylockfile
  </code></pre>
  
  <p>However, the lock is always failing to acquire for subsequent runs (with exit status 1).</p>
  
  <p>Yet <code>fuser /var/lock/mylockfile</code> and <code>lsof /var/lock/mylockfile</code> show nothing.</p>
  
  <p>Now, if I add the command <code>flock -u</code> to manually unlock, like this:</p>
  
  <pre><code>(
      flock -n 9 || exit 1
      # ... commands executed under lock ...
      flock -u 9
  ) 9&gt;/var/lock/mylockfile
  </code></pre>
  
  <p>Then the scripts work.</p>
  
  <p>Am I using <code>flock</code> correctly? Is it safe to call <code>flock -u</code> inside the block?</p>
  
  <p>FYI The troublesome scripts are the ones that call truecrypt within the command block.</p>