No lock file: Preventing multiple instance of a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting No lock file: Preventing multiple instance of a script
# 1  
Old 11-17-2011
No lock file: Preventing multiple instance of a script

I've been bitten by using a lock or pid file to prevent multiple instances of a script. A user typed kill -9, and the pid file didn't go away. You can't trap -9. So when he tried to restart, it said "already running", and I got trouble report. Argh.

So here's what we came up with:
Code:
# Stop if a previous version of this script is still running
ps | grep $0 | grep -v grep > /var/tmp/$0.pid
pids=$(cat /var/tmp/$0.pid | cut -d ' ' -f 1)
for pid in $pids
do
   if [ $pid -ne $$ ]; then
      logprint "$0 is already running. Exiting"
      exit 7
   fi
done
rm -f /var/tmp/$0.pid

It does use a temp file, but leaves no potential artifact to mess us up later.

If you try this without a temp file, you'll end up with a false positive because
pids=$(ps | grep $0 | grep -v grep | cut -d ' ' -f 1)
seems to leave it's own extra pid.

Last edited by Scott; 11-17-2011 at 09:43 PM.. Reason: Please use code tags
# 2  
Old 11-18-2011
Different idea: save the PID of the script in the lock file. If the script is started again, check if that PID is still existing, and in an instance of the script, and stop or run accordingly.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script calling multiple scripts (using lock file)

Hi all, i have compiled a script as below. Basically this script is intended to call 3 other scripts, which i intend to run simultaneously. #!/usr/bin/bash HOMEDIR=/path/of/home LOCKDIR=$HOMEDIR/lock #check for LOCK LOCKFILE=$LOCKDIR/$(basename $0 .sh).lock if ; then echo... (2 Replies)
Discussion started by: nms
2 Replies

2. Ubuntu

How to lock a file through UNIX KSH shell script?

I wrote two shell scripts in UNIX that renames the same file and scheduled them at the same time. The following are the steps that I followed:- 1. I wrote 2 scripts named s1.sh and s2.sh, both trying to add “exec_” prefix to the name of the files present in a folder i which already don't start... (4 Replies)
Discussion started by: piuli
4 Replies

3. Shell Programming and Scripting

script to remove and recreate a lock file

Hi all, i have a small script to remove locks for the prevous day and create new lock for processing in the path on my server i have made something like this #!/bin/sh #lock_remover #script to remove regular lockfiles and hang curr_month=`date "+%b"` 2day=`date "+%_d"` cd... (4 Replies)
Discussion started by: godie_b_w
4 Replies

4. Shell Programming and Scripting

Multiple instance in tomcat

I need to install a tomcat6 with multiple instances like instance1,instance2 and instance3 in a server. I came to know that for that we need to install tomcat6,apache2.0,mod_jk1.2 and jre with tools.jar installed.And we need to create multiple instances with same web.xml and difference... (0 Replies)
Discussion started by: tuxslonik
0 Replies

5. Shell Programming and Scripting

A script that spawns multiple instance of itself.

Hi all,I have some questions of forking new process,the code is below.Any help will be appreciated. 1 #! /bin/bash 2 3 PIDS=$(pidof sh $0) 4 P_array=( $PIDS ) 5 echo $PIDS 6 let "instances = ${#P_array}-1" 7 8 echo "$instances instance(s)" of this script running." 9... (4 Replies)
Discussion started by: homeboy
4 Replies

6. UNIX for Advanced & Expert Users

Multiple Instance of Unix Shell Script

Hi All, I have a problem mentioned below. I have a script which performs line by line operations on several files. I have a temp_file storing the list of names of the file to be validated. Right not in while loop i validate these files one by one. Is there anyway that i can modify... (1 Reply)
Discussion started by: amitaryans
1 Replies

7. Shell Programming and Scripting

Help with multiple instance script checking

I am trying to debug the following script. It appears that when the check for script running occurs, it's finding the actual grep statement and causing the script believe the script is already running. This is deployed on two different servers where one works fine, the other doesn't. Any ideas? ... (2 Replies)
Discussion started by: DaddyMoose
2 Replies

8. UNIX for Advanced & Expert Users

Multiple Instance Of Same Process

Hi Everyone, I am using solaris 5.10. I have a java process running in server mode in unix. The problem is that it automatically forks i.e creates a child process. I mean suddenly two instances of that process start running , in which the process-id of first instance is the parent... (5 Replies)
Discussion started by: glamo_2312
5 Replies

9. UNIX for Dummies Questions & Answers

Multiple instance of same process

;)Hi Everyone, I am using solaris 5.10. I have a java process running in server mode in unix. The problem is that it automatically forks i.e creates a child process. I mean suddenly two instances of that process start running , in which the process-id of first instance is the parent... (0 Replies)
Discussion started by: glamo_2312
0 Replies
Login or Register to Ask a Question