更新时间:2023-12-05 13:29:34
如果所有用户的脚本都相同,您可以使用 lockfile
方法.如果您获得了锁,请继续,否则会显示一条消息并退出.
If the script is the same across all users, you can use a lockfile
approach. If you acquire the lock, proceed else show a message and exit.
举个例子:
[Terminal #1] $ lockfile -r 0 /tmp/the.lock
[Terminal #1] $
[Terminal #2] $ lockfile -r 0 /tmp/the.lock
[Terminal #2] lockfile: Sorry, giving up on "/tmp/the.lock"
[Terminal #1] $ rm -f /tmp/the.lock
[Terminal #1] $
[Terminal #2] $ lockfile -r 0 /tmp/the.lock
[Terminal #2] $
获得/tmp/the.lock
后,您的脚本将是唯一可以执行的脚本.完成后,只需取下锁即可.在脚本形式中,这可能如下所示:
After /tmp/the.lock
has been acquired your script will be the only one with access to execution. When you are done, just remove the lock. In script form this might look like:
#!/bin/bash
lockfile -r 0 /tmp/the.lock || exit 1
# Do stuff here
rm -f /tmp/the.lock