prevent running of duplicate scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting prevent running of duplicate scripts
# 1  
Old 04-23-2010
prevent running of duplicate scripts

i have a script tool.sh,but i want to include a script within it that checks if tool.sh is already running,if already running then the new one called should exit,but if not then the new one should proceed and run.

i want to prevent the duplicate running of the script.
# 2  
Old 04-23-2010
There are partial solutions each one has issues.
1. temporary "lock" file
Code:
 
[[ -f /tmp/tool.sh.lock ]] && exit 1
> /tmp/tool.sh.lock
.....
rm /tmp/tool.sh.lock
exit

2. grep for tool.sh in a process list
Code:
value=""
ps -ef | grep 'tool.sh' | grep -v 'grep' | read value
[[ ! -z $value ]] && exit 1

# 3  
Old 04-23-2010
If you use the lock file technique, you should also put a script in the system start up to remove any existing lock files just in case the system went down while one of these processes was running.
# 4  
Old 04-23-2010
Thank you
# 5  
Old 04-23-2010
or you may look into 'man fuser'.
something along these line:
Code:
#!/bin/ksh

FUSER='/usr/sbin/fuser'
  while :
  do
     myPID="$$"
     echo "My pid->[${myPID}]"
     FUSERout=$(${FUSER} $0 2>/dev/null)
     echo "fuser ->[${FUSERout}]"
     numProc=$(echo "${FUSERout}" | nawk '{print NF}')
     if [ "${numProc}" -gt 1 ]; then
        echo "[$0]: is ALREADY running"
     else
        echo "[$0] is NOT running"
     fi

     sleep 5
     echo "---"
  done

there're multiple threads on this subject - this is one of them.
# 6  
Old 04-23-2010
The simplest i know
Code:
pidof -x -o $$ $(basename "$0") && exit 1

This User Gave Thanks to frans For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Running scripts from different server

Hi, I need a script (ksh) on ServerAdmin that will run an archive scripts from different several Servers through ssh. The problem is that how can i switch user when before running the archive script. I already configured password-less connection on the servers. server 1... (1 Reply)
Discussion started by: chococrunch6
1 Replies

2. Shell Programming and Scripting

running scripts in minicom

Hi, I am new to use minicom. I want script to run on minicom with username and password as automated.(Expect). please could anyone suggest the sample code for it. Thanks in advance (2 Replies)
Discussion started by: vanid
2 Replies

3. AIX

AIX - "prevent script from running twice" issue

I know about standard "ps ..|grep .. | grep -v grep" solution, but... this is different issue I have encountered in several companies I worked for. And I see this only for AIX - not HP, not Solaris, not Linux. Korn shell script is scheduled in the background (via cron /via Tivoli Scheduler or... (6 Replies)
Discussion started by: ooops
6 Replies

4. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

5. Shell Programming and Scripting

Running scripts via su

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin .profile when i execute the command start.sh by logging directly into bin account it's... (8 Replies)
Discussion started by: ravi.sri24
8 Replies

6. Shell Programming and Scripting

Running scripts unattended

Hi guys just wondering how i could make one of my scripts run unattended without the use of cron? (3 Replies)
Discussion started by: musicmancanora
3 Replies

7. UNIX for Dummies Questions & Answers

scripts running under different users

what command can i use to tell if a script is running under different users? (1 Reply)
Discussion started by: csnewbie
1 Replies

8. UNIX for Advanced & Expert Users

How to prevent job1 from running while job2 is running..

Hi, Please I need your expert advise on how to prevent/lock from execution job1 while job2 is still running in Unix... THanks:) (3 Replies)
Discussion started by: tikang
3 Replies

9. UNIX for Dummies Questions & Answers

Running scripts parallely

Hi, Posting my first query in this Forum,here's my query i want to execute 100 .sql files in unix having some code for connecting with db and executing procedures inside that,that to be run parallel like threads.want to run all the 100 .sql files simultanously. thanks in advance. (4 Replies)
Discussion started by: santho
4 Replies

10. Shell Programming and Scripting

Running three scripts parallelly

Hi All, We have three shell script batch, which extract data from three different systems(oracle, db2, db2/400). By running each shell script batch, the data is extracted from respective systems. while the batch is running, job date, system_name, start_date and end_date will be inserted into... (1 Reply)
Discussion started by: anwarsait
1 Replies
Login or Register to Ask a Question