pidof -x $ 0返回2,而不是预期的1,是否有单行select?

在一个bash脚本中,我试图用下面一行来判断另一个进程是否正在执行当前脚本:

/sbin/pidof -x $0 |wc -w

但是响应总是2,而不是1的预期值。

我已经尝试了以下调用:
/sbin/pidof -x $0返回1个PID
/sbin/pidof -x $0 |wc -w返回2
/sbin/pidof -x $0 |head返回2个pid
/sbin/pidof -x $0 |head |wc -w返回2
/sbin/pidof -x $0 |head |tail返回2个pid
/sbin/pidof -x $0 |head |tail |wc -w返回2

任何人都可以提出一个替代(或修复),以实现我想要做的,并解释为什么pipe道输出任何东西导致pidof输出“有点搞笑”?


这是脚本目前使用pidof,它工作正常:

 RUNNING=`/sbin/pidof -x $0` RUNNING=`echo -n ${RUNNING} | wc -w` [[ ${RUNNING} -gt 1 ]] && return 

如果你只是试图阻止第二个运行的脚本,以下将做的伎俩:

 pid=/tmp/$(basename $0).pid [ -e $pid ] && exit 0; trap 'rm -f $pid >/dev/null 2>&1;' 0 trap 'exit 2' 1 2 3 15 touch $pid 

用这些行开始你的脚本。 基本上它检查一个文件,并停止执行,如果该文件存在。 请注意,如果杀死(-9)正在运行的脚本,则负责清理已创建的pid文件。 杀死(-9)绕过将会清理pid文件的shell陷阱。