Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   how to run scripts uniquely (http://www.linux-archive.org/debian-user/3122-how-run-scripts-uniquely.html)

cs 11-22-2007 05:26 PM

how to run scripts uniquely
 
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



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

"Douglas A. Tutty" 11-22-2007 08:49 PM

how to run scripts uniquely
 
On Thu, Nov 22, 2007 at 06:26:40PM +0000, 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 debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

Jeff D 11-22-2007 09:07 PM

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



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org


All times are GMT. The time now is 03:39 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.