Visit The New, Modern Unix Linux Community

How to limit the number of running instances of a script?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to limit the number of running instances of a script?
# 1  
How to limit the number of running instances of a script?

I would like to allow only one instance of a script to run at any moment.

I've tried the following solution to count the instances but the result is always the number of running instances plus one and I can't find the problem

ps -ef | grep $0 | sed '/^$/ d' | sed '/grep/ d' | wc -l

Please help. Thanks!
# 2  

try "grep -v ..." in your command line.

# 3  
Another fairly robust solution is to create a lock file:


# check for existence of a lock file
# if the file exists make sure the pid that 
#          created it is still running then exit.
if [ -e /tmp/myshellscript.lock ]; then 
         pid=`cat /tmp/myshellscript.lock`
         ps -p "$pid" >/dev/null 2&>1
# somebody else is running the script ?
         if [ $? -eq 0]; then
                echo "another processi is running - try again later"
                exit 1
# put the current process as owner of the lock
         echo "$$" > /tmp/myshellscript.lock

# no file found - make one, own the lock,too.

         echo "$$" > /tmp/myshellscript.lock
         chmod 777 /tmp/myshellscript.lock

# last lines of script - release lock file
rm -f /tmp/myshellscript.lock
exit 0

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #1005
Difficulty: Easy
Regarding a circle, one full turn of the circle is equal to 2 radians.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit on number of pipes after long-running command?

I'm trying to create a minimal, crude keylogger for X using only a shell script. I was quickly stumped: Why do these two commands entered in a terminal emulator produce output when I type... $ xinput test 6 | grep press $ xinput test 6 | awk '{print $3}' ...but this command produces no... (13 Replies)
Discussion started by: DevuanFan
13 Replies

2. Shell Programming and Scripting

count the number of instances in 2 columns using awk

Input A.1 Q.1 A.1 Q.2 A.1 Q.3 A.2 Q.4 Explanation: Final Output A.1 Q.1 s1 t1 A.1 Q.2 s1 t2 A.1 Q.3 s1 t3 A.2 Q.4 s5 t5 ---------- Post updated 09-28-12 at 03:38 AM ---------- Previous update was 09-27-12 at 09:10 AM ---------- Hi Guys, I was able to do until... (11 Replies)
Discussion started by: quincyjones
11 Replies

3. Shell Programming and Scripting

A script that kills previous instances of itself upon running not killing child processes

I'm likely going to explain this clumsily, so apologies in advance: I have the following script: #!/bin/bash pidPrefix="logGen" checkPrime () { if /sbin/ifconfig eth0:0|/bin/grep -wq inet;then isPrime=1;else isPrime=0;fi } killScript () { /usr/bin/find /var/run -name... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

4. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND="ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/ >/dev/null 2>&1 &" ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Shell Programming and Scripting

Script counting instances of software running on a machine

Hello to all @here, as Iīm new to this forum, I will try to start in a easy way for my first post. Iīm not beginner in scripting, but also not a proffessional. So please keep easy, if I donīt understand your explanation at once ;) I donīt mean it in a bad way! Here is the Problem: There were... (2 Replies)
Discussion started by: muogli
2 Replies

6. Shell Programming and Scripting

Running script automatically when threshold limit met in one of the field in oracle database

Hi Guys, Need you help in one point! I am working on one shell script which takes following steps : 1. Taking one query result from oracle database 2. Exporting that result to Xls file 3. Mailing that file to my own mail ID Now, I want to give a threshold limit to one of the column... (0 Replies)
Discussion started by: Agupte
0 Replies

7. Shell Programming and Scripting

count and number instances of a character in sed or awk

I currently use LaTeX together with a sed script to set cloze test papers for my students. I currently pepend and equals sign to the front of the words I want to leave out in the finished test, =perpendicular, for example. I am able to number the blanks using a variable in LaTeX. I would like to... (8 Replies)
Discussion started by: maouinin
8 Replies

8. UNIX for Advanced & Expert Users

Limit on Number of Cron Jobs Running on one Account

Hello, I have some 150 Cron Jobs running under my UNIX account. I want to add some more jobs. Is there a limit to the number of cron jobs that can be run on an account? Thank you. Pramodini (8 Replies)
Discussion started by: Pramodini Rode
8 Replies

9. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

10. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: bd_joy
5 Replies

Featured Tech Videos