击:如果只有一个实例运行继续脚本 [英] Bash: Continue script if only one instance is running

查看:70
本文介绍了击:如果只有一个实例运行继续脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,这是令人尴尬的。我在写剧本很快,我想不通,为什么这种说法不工作。

now this is embarrassing. I'm writing quick script and I can't figure out why this statement don't work.

if [ $(pidof -x test.sh | wc -w) -eq 1 ]; then echo Passed; fi

我用,而不是背蜱$(),但它仍然是行不通的也试过。

I also tried using back-ticks instead of $() but it still wouldn't work.

你可以看到什么是错呢? 的pidof -x test.sh | WC -w 返回1​​,如果我运行的脚本里面,所以我看不出有任何理由基本上如果[1 -eq 1] 不会通过。

Can you see what is wrong with it? pidof -x test.sh | wc -w returns 1 if I run it inside of script, so I don't see any reason why basically if [ 1 -eq 1 ] wouldn't pass.

非常感谢!

推荐答案

Jefromi是正确的;这里是逻辑,我认为你想要的:

Jefromi is correct; here is the logic I think you want:

#!/bin/bash
# this is "test.sh"

if [ $(pidof -x test.sh| wc -w) -gt 2 ]; then 
    echo "More than 1"
    exit
fi

echo "Only one; doing whatever..."

这篇关于击:如果只有一个实例运行继续脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆