IT How Tos

Home

About

Sitemap

Contact

How to check if there is already a running instance of the current bash script

  • The script below will check if there is already an instance of the current bash script and will exit if there is
#!/bin/bash

function checkisonlyinstance {
        ruchk=$(ps -ef | grep $basename $0 | egrep -v grep | wc -l)
        rulev="2"
        if [ "$ruchk" -le "$rulev" ]; then
                :
        else
                echo there is already and instance - bye!!
                exit 0
        fi
}

checkisonlyinstance
echo This is the one instance

# continue work here