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

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

    Someone please shoot me down if I'm wrong here

    I understand that the mkdir operation is atomic, so you could create a lock directory

    #!/bin/sh
    lockdir=/tmp/AXgqg0lsoeykp9L9NZjIuaqvu7ANILL4foeqzpJcTs3YkwtiJ0
    mkdir $lockdir  || {
        echo "lock directory exists. exiting"
        exit 1
    }
    # take pains to remove lock directory when script terminates
    trap "rmdir $lockdir" EXIT INT KILL TERM
    
    # rest of script here
    

提交回复
热议问题