The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Identifying and grouping OS processes and APP processes wilsonee SUN Solaris 2 09-30-2008 10:26 AM
Monitoring Processes - Killing hung processes ukndoit UNIX for Advanced & Expert Users 4 01-17-2008 04:30 AM
FTP Files Sequentially harim UNIX for Advanced & Expert Users 1 10-31-2007 10:54 AM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 07:53 AM
processes master_6ez Shell Programming and Scripting 3 11-24-2004 09:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-13-2005
993 993 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 2
Run Processes Sequentially

I've written two shell scripts where script A calls script B, passing two parameters.

Script B takes about a minute to complete (loading and processing data).

When running script B once from script A, everything works ok. However, when running script B more than once from script A, things start to mess up:

e.g with Script A in the format below, the second run starts before the first run has finished.

ScriptB Param1A Param2A
ScriptB Param1B Param2B

Can anyone advise how I can introduce a check to see if script B has completed before starting it again with new parameters?

For info I'm using hp/ux 11.

I'm new to Unix and new to this forum, so thanks for your help in advance.
  #2 (permalink)  
Old 04-13-2005
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Use a lock file that is generated when the first instance of the script is run. The second script will look for the presence of the lock file. If it exists, the second script could either wait (for a specified period of time) or it could exit. After the first script is done, it would of course clean up (remove) the lock file.
I had created a function awhile back to create a lock file based upon the process number of the shell. The function takes two arguments, one for the name of the log file and the other is $$. The script creates a lock file in the /tmp directory. This file holds the process ID of the shell that created it. If you attempt to execute the script while another instance was running, this function will prevent the second instance from running. Works well for me. Feel free to use if you so choose.


CreateLockFile () { unset PROCESS_ID unset ID LFILE=${TEMP_PATH}/${1}_${TODAYS_DATE}.LCK ID=$2 if [ -r ${LFILE} ] then OPID=`cat ${LFILE}` 2> /dev/null if [ -z ${OPID} -eq 0 ] #Make Sure OPID contains a value then exit ${FAILURE} "ERROR-APP-->: `basename ${LFILE}` exists but contains no Process ID" | tee -a ${INLOG} else PROCESS_ID=`ps -p ${OPID} | grep ADD-SCRIPT-NAME-HERE | awk -F" " '{print $1}' 2> /dev/null` if [ ${PROCESS_ID} ] #Lock File is there, check if process is actually running then echo "WARNING-->: ${1} Script Is Currently Running [PID=${OPID}], Exiting. ${DATE_TIME}" | tee -a ${INLOG} exit ${SUCCESS} else echo "INFO-->: Old Lock File with PID= [ ${OPID} ] Exists But Process Is Not Running. " >> ${INLOG} echo "INFO-->: Overwriting Old PID with New PID Value of [ ${ID} ] " >> ${INLOG} echo "$ID" > ${LFILE} fi fi else echo "$ID" > ${LFILE} if [ $? -ne 0 ] then exit ${FAILURE} "ERROR-APP-->: Could Not Create Lock File - Exiting " | tee -a ${INLOG} fi fi }
-->
Code:
CreateLockFile () {

unset PROCESS_ID
unset ID
LFILE=${TEMP_PATH}/${1}_${TODAYS_DATE}.LCK
ID=$2

if [ -r ${LFILE} ]
 then
    OPID=`cat ${LFILE}` 2> /dev/null
    if [ -z ${OPID} -eq 0 ] #Make Sure OPID contains a value
     then
	 exit ${FAILURE} "ERROR-APP-->: `basename ${LFILE}` exists but contains no Process ID" | tee -a ${INLOG}
     else
        PROCESS_ID=`ps -p ${OPID} | grep ADD-SCRIPT-NAME-HERE | awk -F" " '{print $1}'  2> /dev/null`

        if [ ${PROCESS_ID} ]  #Lock File is there, check if process is actually running
         then
           echo "WARNING-->: ${1} Script Is Currently Running [PID=${OPID}], Exiting. ${DATE_TIME}" | tee -a ${INLOG}
           exit ${SUCCESS}
        else
	  echo "INFO-->: Old Lock File with PID= [ ${OPID} ] Exists But Process Is Not Running. " >> ${INLOG}
	  echo "INFO-->: Overwriting Old PID with New PID Value of [ ${ID} ] " >> ${INLOG}
          echo "$ID" > ${LFILE}
        fi 
     fi
else
  echo "$ID" > ${LFILE}

    if [ $? -ne 0 ]
      then 
        exit ${FAILURE} "ERROR-APP-->: Could Not Create Lock File - Exiting " | tee -a ${INLOG}
    fi
fi
}
        

Last edited by google; 04-13-2005 at 11:59 AM..
  #3 (permalink)  
Old 04-14-2005
993 993 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 2
Thanks a lot Google.

That works nicely.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0