📜  bash 检查其他实例 - 无论代码示例

📅  最后修改于: 2022-03-11 14:59:04.587000             🧑  作者: Mango

代码示例1
#!/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}