Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another option is to use shell's <code>noclobber</code> option by running <code>set -C</code>. Then <code>&gt;</code> will fail if the file already exists.</p> <p>In brief:</p> <pre><code>set -C lockfile="/tmp/locktest.lock" if echo "$$" &gt; "$lockfile"; then echo "Successfully acquired lock" # do work rm "$lockfile" # XXX or via trap - see below else echo "Cannot acquire lock - already locked by $(cat "$lockfile")" fi </code></pre> <p>This causes the shell to call:</p> <pre><code>open(pathname, O_CREAT|O_EXCL) </code></pre> <p>which atomically creates the file or fails if the file already exists.</p> <hr> <p>According to a comment on <a href="http://mywiki.wooledge.org/BashFAQ/045" rel="noreferrer">BashFAQ 045</a>, this may fail in <code>ksh88</code>, but it works in all my shells:</p> <pre><code>$ strace -e trace=creat,open -f /bin/bash /home/mikel/bin/testopen 2&gt;&amp;1 | grep -F testopen.lock open("/tmp/testopen.lock", O_WRONLY|O_CREAT|O_EXCL|O_LARGEFILE, 0666) = 3 $ strace -e trace=creat,open -f /bin/zsh /home/mikel/bin/testopen 2&gt;&amp;1 | grep -F testopen.lock open("/tmp/testopen.lock", O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_LARGEFILE, 0666) = 3 $ strace -e trace=creat,open -f /bin/pdksh /home/mikel/bin/testopen 2&gt;&amp;1 | grep -F testopen.lock open("/tmp/testopen.lock", O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_LARGEFILE, 0666) = 3 $ strace -e trace=creat,open -f /bin/dash /home/mikel/bin/testopen 2&gt;&amp;1 | grep -F testopen.lock open("/tmp/testopen.lock", O_WRONLY|O_CREAT|O_EXCL|O_LARGEFILE, 0666) = 3 </code></pre> <p>Interesting that <code>pdksh</code> adds the <code>O_TRUNC</code> flag, but obviously it's redundant:<br> either you're creating an empty file, or you're not doing anything.</p> <hr> <p>How you do the <code>rm</code> depends on how you want unclean exits to be handled.</p> <p><strong>Delete on clean exit</strong></p> <p>New runs fail until the issue that caused the unclean exit to be resolved and the lockfile is manually removed.</p> <pre><code># acquire lock # do work (code here may call exit, etc.) rm "$lockfile" </code></pre> <p><strong>Delete on any exit</strong></p> <p>New runs succeed provided the script is not already running.</p> <pre><code>trap 'rm "$lockfile"' EXIT </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload