Friday, September 30, 2011

Write script that can't be run more than one instance at a time.

#!/bin/sh

if [ -f $0.lock ]; then
        (ps -e | grep $$) >> $0.lock
if [ "$(cat $0.lock | wc -l)" -gt 1 ];then
        echo "\"$0\" is already running with PID=`awk '{print $1}' $0.lock | head -1` !!"
        exit 0
fi
else
        (ps -e | grep $$) > $0.lock
        tn <ServerName>
        rm $0.lock
fi

No comments :