If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Tracking script that is already running

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-14-03, 12:20
jaideep jaideep is offline
Registered User
 
Join Date: Apr 2003
Posts: 54
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
Reply With Quote
  #2 (permalink)  
Old 10-15-03, 08:31
EZEE! EZEE! is offline
Registered User
 
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!
Reply With Quote
  #3 (permalink)  
Old 10-17-03, 03:00
karthi_tvr karthi_tvr is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 10-30-03, 18:13
cdlvj cdlvj is offline
Registered User
 
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.
Reply With Quote
  #5 (permalink)  
Old 11-11-03, 22:20
santi4 santi4 is offline
Registered User
 
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
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On