Something like this works alright:
# Check if process is running already SCRIPT_NAME=do_stuff pidfile=/tmp/$SCRIPT_NAME.pid
if [ -f $pidfile ]; then runpid=`cat $pidfile` if ps -p $runpid; then echo "$SCRIPT_NAME is already running ... stopping" exit 1 else echo "$SCRIPT_NAME pid found but process dead, cleaning up and starting ..." rm -f $pidfile fi else echo "No $SCRIPT_NAME Process Detected ... starting" fi
echo $$ > $pidfile
-----Original Message----- From: centos-bounces@centos.org [mailto:centos-bounces@centos.org] On Behalf Of Morten Torstensen Sent: August 29, 2006 3:20 PM To: CentOS mailing list Subject: Re: [CentOS] Any one have a good example...
Scott Silva wrote:
...of a shell script for rsync that won't start again if it is already
running? I thought of using a lock file, but what if it is killed mid script or bombs?
You write your PID to the lock file, then in startup, you check if the lock file exist and if the PID process is running, then it is still active.
It could be that the process is just hung, so this method will only help assure that you do not start a second copy.