lsof linux命令,用于使用参数值统计正在运行的实例
我使用lsof命令只运行setup.sh的一个实例,如果没有参数值,它可以正常工作。但是,我需要在setup.sh中为例如 setup.sh machine1 setup.sh machine2 setup.sh machine3 因此,如果setup.sh machine1正在运行,并且再次运行相同的命令,它将等待setup.sh machine1完成。我认为lsof是用于过程的,可能不可能,但对于等待逻辑有其他好的建议吗lsof linux命令,用于使用参数值统计正在运行的实例,linux,bash,shell,lsof,Linux,Bash,Shell,Lsof,我使用lsof命令只运行setup.sh的一个实例,如果没有参数值,它可以正常工作。但是,我需要在setup.sh中为例如 setup.sh machine1 setup.sh machine2 setup.sh machine3 因此,如果setup.sh machine1正在运行,并且再次运行相同的命令,它将等待setup.sh machine1完成。我认为lsof是用于过程的,可能不可能,但对于等待逻辑有其他好的建议吗 if ( [[ $(lsof -t \setup.sh 'machin
if ( [[ $(lsof -t \setup.sh 'machine1'| wc -l) > 1 ]] ); then
if ( [[ $(lsof -t '\setup.sh machine1'| wc -l) > 1 ]] ); then
if ( [[ $(lsof -t "\setup.sh machine1"| wc -l) > 1 ]] ); then
当我试图传递lsof命令给出语法错误的某些参数时,我尝试了下面的方法,但没有成功。我的目的是使用machine1只运行一个实例,如果有人尝试使用machine1参数运行setup.sh,那么它应该等待。还有其他文件锁定/等待逻辑吗
if ( [[ $(lsof -t \setup.sh 'machine1'| wc -l) > 1 ]] ); then
if ( [[ $(lsof -t '\setup.sh machine1'| wc -l) > 1 ]] ); then
if ( [[ $(lsof -t "\setup.sh machine1"| wc -l) > 1 ]] ); then
工作:
不工作(语法错误):
您可以使用
ps-eo args
搜索具有参数的进程,如下所示
ps -eo args | grep '^setup.sh machine1' | wc -l
您需要更改grep模式以适应您的情况。Greg的Wiki有一篇关于流程管理的文章,特别是“
lsof
不是保证只有一个进程运行的可靠方法,因为在检查lsof
的输出和继续之间存在竞争条件。如果两个进程几乎同时启动,它们很可能会并行运行。为什么在setup.sh
之前有正斜杠?它只是当前放置/执行脚本的路径。
ps -eo args | grep '^setup.sh machine1' | wc -l