确保给定脚本只有一个实例在运行的最简单/最好的方法是什么 - 假设它是 Linux 上的 Bash?
目前我正在做:
ps -C script.name.sh > /dev/null 2>&1 || ./script.name.sh
但它有几个问题:
- 它将检查置于脚本之外
- 它不允许我从不同的帐户运行相同的脚本 - 我有时会这样做。
-C
只检查进程名的前 14 个字符
当然,我可以编写自己的 pidfile 处理,但我觉得应该有一个简单的方法来做到这一点。
最佳答案
建议锁定已经使用了很长时间,并且可以在 bash 脚本中使用。我更喜欢简单的 flock
(来自 util-linux[-ng]
)而不是 lockfile
(来自 procmail
)。并且永远记住那些脚本中的退出陷阱(sigspec == EXIT
或 0
,捕获特定信号是多余的)。
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.
关于linux - 确保只有一个 Bash 脚本实例正在运行的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1715137/