Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Posts
    54

    Unanswered: Tracking script that is already running

    Hi,
    I have a script which wakes up twice every hour 5,30 and looks for any new file and if it finds one, it decompresses it and applies the file to our warm standby database. Now, sometimes when I try and load a big file, it takes one hour to load it and in the meantime, the same script fires again.So, what is a good way to check whether an older instance of the script is already running or not and if it finds one then just exits out??? I tried grepping the script name with ps -ef and then awking the 9th column but if anyone else has the file open for reading and also the file is running then ps -ef script returns two for the search. Please help !!!!!!!!!..............

    Regards

    Subhas

  2. #2
    Join Date
    Oct 2003
    Location
    East Coast of South Africa
    Posts
    10
    In your script move the file to another directory, I call mine bla/working
    When ,after an hour, the script starts again, it will not find the file, if it does find a file, it will be a new file. When all is complete I move the file to bla/archiv and gzip it . All along I echo results to a bla/log directory.

    EZEE!

  3. #3
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Create a LOCK file using "echo $$ > $LOCK", so that you can get the process id of the script.
    when you enter the script, check if the LOCK file exists, if yes then cat the file and do a ps -ef for that process id. If process is running, just exit. Else create the lock. Once the script completes, remove the lock. If lock file does not already exist when starting, create it and continue. Something like this :-

    LOCK=/xyz/file.lck
    if [ -f $LOCK ]; then
    PID=`cat $LOCK`
    PROC=`ps -ef | grep $PID | grep -v grep`
    if [ " $PROC" = " " ]; then
    echo $$ > $LOCK
    else
    exit 0
    fi
    else
    echo $$ > $LOCK
    fi

    Instead of using PID, you can just touch a lock and remove it....but the problem occurs if the script starts, touches a lock and abruptly terminates....all further invoke of the script think that it is already running and exit..
    Thanks and Regards
    Karthik R

  4. #4
    Join Date
    Oct 2003
    Location
    San Antonio, Texas
    Posts
    4

    Similar to the lock method

    Create a file with no permissions. When the script fires up, set to chmod one of the permission r/w/x, but 1st of all do a test if that permission is set, and fail appropriately.


    I used this for all my online programs. For instance set to read, then online programs could see the database but no updates. Set to X, and then no online programs were allowed to run. Normal state would be for W permissions. Lot of possibilities here.

  5. #5
    Join Date
    Nov 2003
    Location
    Ortigas, Manila Philippines
    Posts
    10
    In your script add another function which checks if there are old process running...

    insert this function above your script... this function checks process "compress" and if it is still running.. it sleeps and checks again..
    until the compressing finished and it will continue the next function..

    I hope this will solve your problem..

    checkcompress() {
    while :
    do
    ps -ef | grep compress | grep -v grep > /tmp/comsan.txt
    if [ -s /tmp/comsan.txt ]; then
    echo "DB was still compressing files... Please wait!"
    sleep 10
    else
    return 0
    fi
    done
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •