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

后端 未结 15 2031
执念已碎
执念已碎 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:32

    I find the answer from @Austin Phillips is spot on. One small improvement I'd do is to add -o (to ignore the pid of the script itself) and match for the script with basename (ie same code can be put into any script):

    if pidof -x "`basename $0`" -o $$ >/dev/null; then
        echo "Process already running"
    fi
    

提交回复
热议问题