How to check if another instance of my shell script is running

后端 未结 15 2066
执念已碎
执念已碎 2020-12-01 00:35

GNU bash, version 1.14.7(1)

I have a script is called \"abc.sh\" I have to check this from abc.sh script only... inside it I have written f

15条回答
  •  生来不讨喜
    2020-12-01 01:18

    I have found that using backticks to capture command output into a variable, adversly, yeilds one too many ps aux results, e.g. for a single running instance of abc.sh:

    ps aux | grep -w "abc.sh" | grep -v grep | wc -l
    

    returns "1". However,

    count=`ps aux | grep -w "abc.sh" | grep -v grep | wc -l`
    echo $count
    

    returns "2"

    Seems like using the backtick construction somehow temporarily creates another process. Could be the reason why the topicstarter could not make this work. Just need to decrement the $count var.

提交回复
热议问题