Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Advisory locking has been used for ages and it can be used in bash scripts. I prefer simple <code>flock</code> (from <code>util-linux[-ng]</code>) over <code>lockfile</code> (from <code>procmail</code>). And always remember about a trap on exit (sigspec == <code>EXIT</code> or <code>0</code>, trapping specific signals is superfluous) in those scripts.</p> <p>In 2009 I released my lockable script boilerplate (originally available at my wiki page, nowadays available as <a href="https://gist.github.com/571091" rel="noreferrer">gist</a>). Transforming that into one-instance-per-user is trivial. Using it you can also easily write scripts for other scenarios requiring some locking or synchronization.</p> <p>Here is the mentioned boilerplate for your convenience.</p> <pre><code>#!/bin/bash # SPDX-License-Identifier: MIT ## Copyright (C) 2009 Przemyslaw Pawelczyk &lt;przemoc@gmail.com&gt; ## ## This script is licensed under the terms of the MIT license. ## https://opensource.org/licenses/MIT # # Lockable script boilerplate ### HEADER ### LOCKFILE="/var/lock/`basename $0`" LOCKFD=99 # PRIVATE _lock() { flock -$1 $LOCKFD; } _no_more_locking() { _lock u; _lock xn &amp;&amp; rm -f $LOCKFILE; } _prepare_locking() { eval "exec $LOCKFD&gt;\"$LOCKFILE\""; trap _no_more_locking EXIT; } # ON START _prepare_locking # PUBLIC exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail exlock() { _lock x; } # obtain an exclusive lock shlock() { _lock s; } # obtain a shared lock unlock() { _lock u; } # drop a lock ### BEGIN OF SCRIPT ### # Simplest example is avoiding running multiple instances of script. exlock_now || exit 1 # Remember! Lock file is removed when one of the scripts exits and it is # the only script holding the lock or lock is not acquired at all. </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