linux - 确保只有一个 Bash 脚本实例正在运行的最佳方法是什么?

确保给定脚本只有一个实例在运行的最简单/最好的方法是什么 - 假设它是 Linux 上的 Bash?

目前我正在做:

ps -C script.name.sh > /dev/null 2>&1 || ./script.name.sh

但它有几个问题:

  1. 它将检查置于脚本之外
  2. 它不允许我从不同的帐户运行相同的脚本 - 我有时会这样做。
  3. -C 只检查进程名的前 14 个字符

当然,我可以编写自己的 pidfile 处理,但我觉得应该有一个简单的方法来做到这一点。

最佳答案

建议锁定已经使用了很长时间,并且可以在 bash 脚本中使用。我更喜欢简单的 flock(来自 util-linux[-ng])而不是 lockfile(来自 procmail)。并且永远记住那些脚本中的退出陷阱(sigspec == EXIT0,捕获特定信号是多余的)。

2009 年,我发布了我的可锁定脚本样板(最初在我的 wiki 页面上可用,现在以 gist 的形式提供)。将其转换为每个用户一个实例是微不足道的。使用它,您还可以轻松地为需要锁定或同步的其他场景编写脚本。

为了您的方便,这里是提到的样板。

#!/bin/bash
# SPDX-License-Identifier: MIT

## Copyright (C) 2009 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## 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 && rm -f $LOCKFILE; }
_prepare_locking()  { eval "exec $LOCKFD>\"$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.

https://stackoverflow.com/questions/1715137/

相关文章:

linux - CURL 用于访问需要从其他页面登录的页面

python - 字典:获取键列表的值列表

python - isinstance ('aaa' , basestring) 和 isinsta

linux - 如何在 GNU/Linux 上设置 Subversion (SVN) 服务器 - U

python - 在 Python 脚本中,如何设置 PYTHONPATH?

python - 如何在python中向后循环?

unix - 创建守护进程时执行双叉的原因是什么?

python - 如何处理boto3的错误?

linux - 使用 Git 保留文件权限

python - 如何以正确的方式平滑曲线?