Script that checks for previous instances running

Hello,
I'm trying to write a script that checks for previous instances of the same script which may still be running (this script is scheduled to run every 30 minutes). I want to somehow use the pid from each instance to make sure the previous one isn't running before continuing with my processing, and wait for a set time period before checking again. I'd also like it to only retry a certain number of times before exiting with a failure notification.

I've never used a scripts pid before but one of my unix admins said that I can get the pid of the process by running a "echo $$" within the script.

Any suggestions to be appreciated. Right now I just have an e-mail notification at the end of my script with a date stamp so I can see (every 30 minutes thru the business day) that the script finished before the next one was scheduled to start - which is far from idea. :frowning:

EDIT: I should note that this runs on AIX 5.3 and using ksh.

One way that does not use a pid - assuming the scrpit filename is somewhat unique:

ps -ef | grep ' <scriptfile> ' | grep -v ' <scriptfile >'

A simpler way to is to have a gentleman's agreement on a lock file, if this script has a very low probablility of failure.

When your script starts it looks in a "fixed file name" for 'I finished'. If it finds that, then it changes the file to read 'I am running', runs, then on exit resets the file to 'I finished'.

On startup, if the script finds 'I am running', it can exit with a note in the log file saying the other guy was still running. It can also wait, then check again as many times as you want.

look into 'man fuser' if you have one on AIX.

I'm not sure what more this shows between the two:

$ ps -ef |grep myscript.sh |grep -v grep
djoy3 606700 688184 0 09:27:57 pts/0 0:00 /bin/sh ./myscript.sh
$ fuser -u myscript.sh
myscript.sh: 606700(djoy3)

They both output the pid.

I guess there really isn't an easy way to do this. I was trying to avoid using start and end trigger files. Does anyone have any other suggestions on this? Thanks for your help!

I'm confused - you wanted a pid, I thought.

Instead of telling us how you want to do it, like find a pid, how about file existence?

#!/bin/ksh
if [[ ! -f /tmp/myscript.sh.lock ]] ; then  # is file not there 
    echo "1" >  /tmp/myscript.sh.lock   # no, make one and proceeed with script
else
    echo "Another myscript still running."  # exit, file is still there
    exit 1
fi
# ............. do stuff
rm -f /tmp/myscript.sh.lock     # remove the file so the next gut can run
exit 0

I do not see why an approach like this will not work. It does have issues if your script fails a lot or if you reboot during the time the script is running. But. You can easily add an ignore option, myscript.sh -I, so that it runs regardless of the exitence of the file.

Thank you for your input. This is what I think I'm going to do. I can't get the counter to work right, but I'll keep messing with it.

#!/bin/sh
EMAIL1="djoy3@nomail.com"
count=0
until ls myscript2.sh.trg >>/dev/null 2>&1
do
count=count+1
if [ count -gt 16 ]
then
echo "Max wait time exceeded for myscript2.sh job" | mailx -s "myscript2.sh job error - `date`" ${EMAIL1}
exit 1
fi
sleep 20
done
#do other processing here
echo "Other processing running"
touch myscript2.sh.trg