Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

bash check other instances

#!/bin/bash

BNAME=$(basename $0)
LOCKFILE=/var/lock/$BNAME
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
    echo "already running"
    exit
fi

# make sure the lockfile is removed when we exit and then claim it
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
echo $$ > ${LOCKFILE}

# do stuff
sleep 1000

rm -f ${LOCKFILE}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #bash #check #instances
ADD COMMENT
Topic
Name
9+6 =