Avoiding the second run of the script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Avoiding the second run of the script
# 1  
Old 11-04-2011
Avoiding the second run of the script

Hi all,

I want to put a check in my script to check if the same instance is already running and not finished and if not then does not allow it to run! in which part of my script I should put this? and any idea how I should write it? tx
# 2  
Old 11-04-2011
Your question confuses me a little. But the short answer is:

Create a lock file. This file has one single entry, the pid of the process running your script. If the file is "blank" then no script is running. If the file has a pid, but the pid (if it exists) is not running your script, then the previous version of the script crashed.

Make SURE you have only one place, one exit point for a succesful completion.

Put two entries the the file:
this one goes at the very beginning - this will work in ksh or bash:
Code:
export MYLOCKFILE=/path/to/mylockfile
[ ! -f  $MYLOCKFILE ]   &&  > $MYLOCKFILE    # create the file it it does not exist
pid=$(cat $MYLOCKFILE)
if [ -z  "$pid" ] ; then
   echo $$ > $MYLOCKFILE       # ok to run
else
   ps -ef | grep $pid | grep -q  $0  # see if this script is running
   if [ $? -eq 0 ] ; then   
         exit 1                        # error exit; another process is now running the script
   else
        echo ' this is a restart'
        echo $$ > $MYLOCKFILE       # ok to run
fi


one line before the one and only successful exit point:
Code:
echo "" > $MYLOCKFILE    # clear the lock

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 11-04-2011
With some limitations.....
Code:
#!/bin/ksh
#

thisFILE="$(whence ${0})"
progName="${0##*/}"

     myPID="$$"

     FUSERout=$(fuser ${thisFILE} 2>/dev/null)
     typeset -i numProc=$(echo "${FUSERout}" | nawk '{print NF}')
     if [ "${numProc}" -gt 1 ]; then
        echo "${progName}: another instance(s) of [${thisFILE}] is currently still running [$(echo ${FUSERout} | sed -e 's/  */ /g')] - exiting THIS [${myPID}] run."
        exit 1
     fi
#
# here comes the rest of the script

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 11-04-2011
can I just do this?

Code:
#!/bin/sh
if [ $(ps -ef | grep "/myapps/app1/try/messi/cz/t.sh" | grep -v grep | wc -l) -gt 1 ]
then
        echo "Im running $(date)...exiting "
        exit 1
fi

# 5  
Old 11-11-2011
Jim,

what does this mean?
Code:
pid=$(cat $MYLOCKFILE) if [ -z  "$pid" ] ; then    echo $$ > $MYLOCKFILE       # ok to run


and this:

Code:
 else         echo ' this is a restart'         echo $$ > $MYLOCKFILE       # ok to run

I need some more explanation, would you? thanks

---------- Post updated at 06:32 AM ---------- Previous update was at 04:27 AM ----------

I understood them myself.
can I ask a general question?
Guys I always have a starting and kick off problem!! on how to start a script?!!! I have some skills to write the script but the kick-off is always a problem for me!!! Smilie Smilie Smilie
this really makes me sad!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

2. Shell Programming and Scripting

Avoiding new line for the counts

Hi Team, Am getting the below output but need the count of records to be displayed in same line but currently count alone moves to next line. Please let me know how we can still keep the count in the same line. ######code ##### while read YEAR; do for i in TEST_*PGYR${YEAR}_${DT}.csv; do... (3 Replies)
Discussion started by: weknowd
3 Replies

3. Shell Programming and Scripting

Script fails to run properly when run from CRONTAB

Hello all, I'm trying to write a script to gather and send data and it works just fine at the bash command line, but when executing from CRON, it does not run properly. My scripting skills are pretty limited and there's probably a better way, but as I said it works at the command line, but... (12 Replies)
Discussion started by: rusman
12 Replies

4. UNIX for Dummies Questions & Answers

Avoiding the history

In bash shell, how we can avoid the commands getting recorded in history file. One way i can think of is : export HISTSIZE=0 Is there any other way to achieve this? Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

5. Shell Programming and Scripting

Avoiding 'sh -c' when running ps from CRON

Hi, I have a script which has the below line: ps -ef | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp When the script is invoked through CRON, I get 2 lines in instance.tmp when actually only one instance is running: cdrd 17790 17789 0 15:14:01 ? 0:00 /bin/ksh... (8 Replies)
Discussion started by: cavallino4u
8 Replies

6. Shell Programming and Scripting

how to run an already made script run against a list of ip addresses solaris 8 question

how to run an already developed script run against a list of ip addresses solaris 8 question. the script goes away and check traffic information, for example check_GE-VLANStats-P3 1.1.1.1 and returns the results ok. how do I run this against an ip list? i.e a list of 30 ip addresses (26 Replies)
Discussion started by: llcooljatt
26 Replies

7. Shell Programming and Scripting

Avoiding For Loop in Shell Script

I am looking to a solution to the following problem. I have a very large file that looks something like this: Each group of three numbers on each line are three probabilities that sum to one. I want to output the maximum for each group of three. So desired output would be: or... (6 Replies)
Discussion started by: hydrabane
6 Replies

8. AIX

My script didn't run every run every minute at cronjob

In my cronjob, I would like to schedule my script.sh to run every minutes. I crontab -e and have in line below but it didn't seems to run at all. * * * * * script.sh When I run it manually, I can run it. Is that anything wrong with the above line? If I change it to something like below,... (4 Replies)
Discussion started by: ngaisteve1
4 Replies

9. UNIX for Advanced & Expert Users

script to run different shells which run different processes

Hi, Would like to ask the experts if anyone knows how to run a script like this: dtterm -title shell1 run process1 on shell1 dtterm -title shell2 run process2 on shell2 cheers! p/s: sorry if i used the wrong forum, quite concussed after watching world cup for several nights; but I... (2 Replies)
Discussion started by: mochi
2 Replies
Login or Register to Ask a Question