how to run scripts uniquely

2007-11-22 Thread cs
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 [EMAIL PROTECTED];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'
[EMAIL PROTECTED]
+ rm /tmp/chk_procs_220
+ exit -1



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: how to run scripts uniquely

2007-11-22 Thread Douglas A. Tutty
On Thu, Nov 22, 2007 at 06:26:40PM +, 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?

Whenever I've needed to do that, I follow the 'standard' method with a
file in /var/lock.  Create a file with your script name and put its PID
in it.  Your script can then test for the existance of this file, read
it, and verify the existance of the PID (remove the file if the PID is
dead) and create its own lock file, removing it on successfull
completion.


Doug.


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: how to run scripts uniquely

2007-11-22 Thread Jeff D

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 [EMAIL PROTECTED];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'
[EMAIL PROTECTED]
+ 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 21 ; 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



--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]