The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
"find command" to find the files in the current directories but not in the "subdir" swamymns Shell Programming and Scripting 9 07-22-2008 12:23 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
Unix "at" / "Cron" Command New Problem...Need help Mohanraj UNIX for Dummies Questions & Answers 3 01-26-2006 08:08 PM
Breaking input with "read" command vino Shell Programming and Scripting 2 08-04-2005 01:10 PM
how to request a "read" or "delivered" receipt for mails plelie2 Shell Programming and Scripting 1 08-06-2002 04:26 PM

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 11-12-2003
435 Gavea 435 Gavea is offline
Registered User
  
 

Join Date: Oct 2003
Location: Brazil
Posts: 42
Arrow The "read" command

Hello there.

I'm trying do make a script capable of verifying if a given script is running at the moment in the environment, and if does, forbid it to execute.

So, I've coded this so far:

Code:
#!/bin/ksh
set CUR_SHELL="/bin/ksh"
set V1="VAR1"
set V2="VAR2"

ps -e -o args | grep "$CUR_SHELL $1" | tr -s " " | cut -d" " -f1 | 
while read -r V1 V2
do
    if [ "$V1" = "$CUR_SHELL" ]; then
        echo "Error - Program is already running."
        echo "V1 -> $V1"
        exit 1
    fi 
done
echo "V1 -> $V1"
echo "V2 -> $V2"

if [ -z "$V1" ]; then
    echo "V1 IS NULL"
fi

I was trying to encapsulate the ps and its pipes into a variable, then to use it like
while read $COMMAND V1 V2 (the second variable exists only for testing purposes)

or while read ....
do

done < $COMMAND

But I got into a endless loop.
Also, I got into the first if and the variable V1 printed out was displaying NULL...

Does anyone have any suggestion, tip or corrections to this script ?

Thanks in advance,
435 Gavea - bRaZiL - thE hElL iS herE !!!

added code tags for readability --oombera

Last edited by oombera; 02-21-2004 at 02:18 AM..
  #2 (permalink)  
Old 11-12-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
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.

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
}

added code tags for readability --oombera

Last edited by oombera; 02-21-2004 at 02:19 AM..
  #3 (permalink)  
Old 11-12-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Don't do:
set var="string"
in ksh. It's just
var="string"
  #4 (permalink)  
Old 11-13-2003
435 Gavea 435 Gavea is offline
Registered User
  
 

Join Date: Oct 2003
Location: Brazil
Posts: 42
Quote:
Originally posted by Perderabo
Don't do:
set var="string"
in ksh. It's just
var="string"
Using set doesn't make the code more readable ?
  #5 (permalink)  
Old 11-13-2003
435 Gavea 435 Gavea is offline
Registered User
  
 

Join Date: Oct 2003
Location: Brazil
Posts: 42
Thanks, google

Well, I've heard about some solutions using a temporary file as well, but
my "boss" said to avoid this option if I can.

So I'm trying to figure out a different solution...
But I didn't thaught about using the PID ... it may be a good point...

Thanks anyway !
  #6 (permalink)  
Old 11-13-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Quote:
Originally posted by 435 Gavea
Using set doesn't make the code more readable ?
Using set makes the code wrong.
var="string"
will set $var to the value "string". On the other hand,
set var=string
does not affect a variable called var. Instead it clobbers the arguments and sets $1 to the value "var=string". Try it,
set var="string"
echo $var
echo $1
  #7 (permalink)  
Old 11-13-2003
435 Gavea 435 Gavea is offline
Registered User
  
 

Join Date: Oct 2003
Location: Brazil
Posts: 42
Thumbs up Interesting !

So...
What's actually the purpose of the set command ?
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 02:41 PM.


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