[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to run scripts uniquely



cs wrote:
I thought I'd worked out how to ensure only 1 instance of a shell script
was running (using grep and wc) but I now see that sometimes the script
name appears twice in the list of processes - any ideas?

eg (apols for wrapping) when running /home/michael/bin/backup-rsync-VERI
we get the below where it appears twice in the list of processes...

++ mktemp /tmp/chk_procs_XXX
+ TEMPFILE=/tmp/chk_procs_220
+ trap 'date|mail -s "$0 error" michael.bane@manchester.ac.uk;echo Error
- aborting; exit' ERR
+ ps -elf
+ grep /home/michael/bin/backup-rsync-VERI
+ grep -v grep
+ tee /tmp/chk_procs_220
4 R michael  20219 20218  0  83   0 -  1020 -      18:18 ?
00:00:00 /bin/bash -x /home/michael/bin/backup-rsync-VERI
1 R michael  20223 20219  0  83   0 -  1020 -      18:18 ?
00:00:00 /bin/bash -x /home/michael/bin/backup-rsync-VERI
++ cat /tmp/chk_procs_220
++ wc -l
+ [[ 2 -gt 1 ]]
+ date
++ hostname -s
+ mail -s 'ratty /home/michael/bin/backup-rsync-VERI - already running'
michael.bane@manchester.ac.uk
+ rm /tmp/chk_procs_220
+ exit -1





This is what I do in situations like this:
#!/bin/sh
#get pid
PID=$$

#check to see if a previous process is running
if [ -e  /var/run/sync.check ] ; then
        if ps -p `cat /var/run/sync.check` >> /dev/null 2>&1 ; then
                echo "Sync process already running, exiting"
                exit 1
        fi
fi
#write out pid
echo $PID > /var/run/sync.check

#do stuff

#end of script, remove pid file
rm -f /var/run/sync.check




Reply to: