Unset variables in shell when it running two different loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unset variables in shell when it running two different loops
# 1  
Old 05-05-2013
Unset variables in shell when it running two different loops

I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the startup after stopping the tomcat),tried to unset the variables after stopping the tomcat but it doesn't help.
Code:
#!/bin/bash
#
# tomcat6      This shell script takes care of starting and stopping Tomcat


if [ -r /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    exit 1
fi

DISTRIB_ID=`lsb_release -i -s 2>/dev/null`

NAME="$(basename $0)"
unset ISBOOT
if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
    NAME="${NAME:3}"
    ISBOOT="1"
fi

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser"
else
    SU="/bin/su"
fi

# Get the tomcat config (use this for environment specific settings)
TOMCAT_CFG="/etc/tomcat6/tomcat6.conf"
if [ -r "$TOMCAT_CFG" ]; then
    . $TOMCAT_CFG
fi
# Should I turn on dynatrace?
if [ -r "/etc/sysconfig/dtagent" ]; then
   . /etc/sysconfig/dtagent
fi

# Get instance specific config file
if [ -r "/etc/sysconfig/${NAME}" ]; then
    . /etc/sysconfig/${NAME}
fi

# Define which connector port to use
CONNECTOR_PORT="${CONNECTOR_PORT:-8080}"

# Path to the tomcat launch script
TOMCAT_SCRIPT="/usr/sbin/tomcat6"

# Tomcat program name
TOMCAT_PROG="${NAME}"

# Define the tomcat username
TOMCAT_USER="${TOMCAT_USER:-tomcat}"

# Define the tomcat log file
TOMCAT_LOG="${TOMCAT_LOG:-/var/log/tomcat6/catalina.out}"

RETVAL="0"

# Look for open ports, as the function name might imply
function findFreePorts() {
    local isSet1="false"
    local isSet2="false"
    local isSet3="false"
    local lower="8000"
    randomPort1="0"
    randomPort2="0"
    randomPort3="0"
    local -a listeners="( $(
                        netstat -ntl | \
                        awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}'
                    ) )"
    while [ "$isSet1" = "false" ] || \
          [ "$isSet2" = "false" ] || \
          [ "$isSet3" = "false" ]; do
        let port="${lower}+${RANDOM:0:4}"
        if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then
            if [ "$isSet1" = "false" ]; then
                export randomPort1="$port"
                isSet1="true"
            elif [ "$isSet2" = "false" ]; then
                export randomPort2="$port"
                isSet2="true"
            elif [ "$isSet3" = "false" ]; then
                export randomPort3="$port"
                isSet3="true"
            fi
        fi
    done
}

function makeHomeDir() {
    if [ ! -d "$CATALINA_HOME" ]; then
        echo "$CATALINA_HOME does not exist, creating"
        if [ ! -d "/usr/share/${NAME}" ]; then
            mkdir /usr/share/${NAME}
            cp -pLR /usr/share/tomcat6/* /usr/share/${NAME}
        fi
        mkdir -p /var/log/${NAME} \
                 /var/cache/${NAME} \
                 /var/tmp/${NAME}
        ln -fs /var/cache/${NAME} ${CATALINA_HOME}/work
        ln -fs /var/tmp/${NAME} ${CATALINA_HOME}/temp
        cp -pLR /usr/share/${NAME}/bin $CATALINA_HOME
        cp -pLR /usr/share/${NAME}/conf $CATALINA_HOME
        ln -fs /usr/share/java/tomcat6 ${CATALINA_HOME}/lib
        ln -fs /usr/share/tomcat6/webapps ${CATALINA_HOME}/webapps
        chown ${TOMCAT_USER}:${TOMCAT_USER} /var/log/${NAME}
    fi
}

function parseOptions() {
    options=""
    # Should I turn on dynatrace?
    if [ "x$DTAGENT" == "xtrue" ] && [ "x$DTCOLLECTOR" != "x" ]; then
       options="export CATALINA_OPTS=-agentpath:/usr/share/dtagent/libdtagent.so=name=${NAME},server=${DTCOLLECTOR},logpath=/var/log/cmates,logfile=${NAME}-dtagent.log"
    fi

    if [ -r "/etc/tomcat6/tomcat6.conf" ]; then
       options="$options $(
                 awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
                 /etc/tomcat6/tomcat6.conf
             )"
    fi
    if [ -r "/etc/sysconfig/${NAME}" ]; then
        options="$options $(
                     awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
                     /etc/sysconfig/${NAME}
                 )"
    fi
    TOMCAT_SCRIPT="$options ${TOMCAT_SCRIPT}"
}


function start() {
    echo -n "Starting ${TOMCAT_PROG}: "
    if [ -f "/var/lock/subsys/${NAME}" ] ; then
        if [ -f "/var/run/${NAME}.pid" ]; then
            read kpid < /var/run/${NAME}.pid
#           if checkpid $kpid 2>&1; then
            if [ -d "/proc/${kpid}" ]; then
                log_success_msg
                if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
                    echo
                fi
                return 0
            fi
        fi
    fi
    # fix permissions on the log and pid files
    export CATALINA_PID="/var/run/${NAME}.pid"
    touch $CATALINA_PID
    chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID
    touch $TOMCAT_LOG
    chown ${TOMCAT_USER}:${TOMCAT_USER} $TOMCAT_LOG
    if [ "$CATALINA_BASE" != "/usr/share/tomcat6" ]; then
        # Create a tomcat directory if it doesn't exist
        makeHomeDir
        # If CATALINA_HOME doesn't exist modify port number so that
        # multiple instances don't interfere with each other
        #findFreePorts
        sed -i -e "s/8005/1${CONNECTOR_PORT}/g" -e "s/8080/${CONNECTOR_PORT}/g" \
            -e "s/8009/2${CONNECTOR_PORT}/g" -e "s/8443/3${CONNECTOR_PORT}/g" \
            ${CATALINA_BASE}/conf/server.xml
    fi
    parseOptions
    if [ "$SECURITY_MANAGER" = "true" ]; then
        $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security" \
            >> $TOMCAT_LOG 2>&1
    else
        $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start" >> $TOMCAT_LOG 2>&1
    fi
    RETVAL="$?"
    if [ "$RETVAL" -eq 0 ]; then
        log_success_msg
        touch /var/lock/subsys/${NAME}
    else
        log_failure_msg
    fi
    if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
        echo
    fi
    return $RETVAL
}

function stop() {
    RETVAL="0"
    echo -n "Stopping ${TOMCAT_PROG}: "
    if [ -f "/var/lock/subsys/${NAME}" ]; then
        if [ "$CATALINA_BASE" != "/usr/share/tomcat6" ]; then
           sed -i -e "s/8005/1${CONNECTOR_PORT}/g" -e "s/8080/${CONNECTOR_PORT}/g" \
               -e "s/8009/2${CONNECTOR_PORT}/g" -e "s/8443/3${CONNECTOR_PORT}/g" \
               ${CATALINA_BASE}/conf/server.xml
        fi
        parseOptions
        echo -n "TDump in ${TOMCAT_PROG}.catalina.out log"
        if [ -f "/var/run/${NAME}.pid" ]; then
            read kpid < /var/run/${NAME}.pid
            kill -3 $kpid
        fi
        $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop" >> $TOMCAT_LOG 2>&1
        #RETVAL="$?"
        #if [ "$RETVAL" -eq "0" ]; then
            count="0"
            if [ -f "/var/run/${NAME}.pid" ]; then
                read kpid < /var/run/${NAME}.pid
                until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
                      [ "$count" -gt "$SHUTDOWN_WAIT" ]; do
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
                        echo "waiting for processes $kpid to exit"
                    fi
                    sleep 1
                    let count="${count}+1"
                done
                if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
                        echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
                    fi
                    kill -9 $kpid
                fi
                log_success_msg
            fi
            rm -f /var/lock/subsys/${NAME} /var/run/${NAME}.pid
        #else
        #    log_failure_msg
        #fi
    else
        log_success_msg
    fi
    if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
        echo
    fi
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        unset CATALINA_OPTS options
        start
        ;;
    condrestart|try-restart)
        if [ -f "/var/run/${NAME}.pid" ]; then
            stop
            start
        fi
        ;;
    reload)
        RETVAL="3"
        ;;
   force-reload)
        if [ -f "/var/run/${NAME}.pid" ]; then
            stop
            start
        fi
        ;;
    status)
        if [ -f "/var/run/${NAME}.pid" ]; then
#           status ${NAME}
#           RETVAL="$?"
            read kpid < /var/run/${NAME}.pid
            if [ -d "/proc/${kpid}" ]; then
                echo "${NAME} (pid ${kpid}) is running..."
                RETVAL="0"
            fi
        else
            pid="$(/usr/bin/pgrep -f -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java.*${NAME}.*)"
            if [ -z "$pid" ]; then
#               status ${NAME}
#               RETVAL="$?"
                echo "${NAME} is stopped"
                RETVAL="3"
            else
                echo "${NAME} (pid $pid) is running..."
                RETVAL="0"
            fi
        fi
        ;;
    version)
        ${TOMCAT_SCRIPT} version
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|configupdate|condrestart|try-restart|reload|force-reload|status|version}"
        RETVAL="2"
esac

exit $RETVAL


Last edited by Franklin52; 05-06-2013 at 03:30 AM.. Reason: Please use code tags
# 2  
Old 05-05-2013
Please go back and edit for those code tags. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Updating variables in a running shell process

Hi I was hoping some one might be able to help me with my problem. I am trying to write a script that will help organize our print server by identifying when a new file has arrived and deleting the older version. I have most of the code written that I need although I still have one small... (2 Replies)
Discussion started by: Paul Walker
2 Replies

2. Shell Programming and Scripting

Listing all local variables for unset

I have tried to thoroughly search other threads before posting this question... I have a shell script (bsh) that I'd like to "re-execute" if the user chooses to. Before the program is executed again the local variables (those set within the script) need to be unset. Is there a command that... (6 Replies)
Discussion started by: powwm
6 Replies

3. Shell Programming and Scripting

Two variables in output file name nested for loops

I am trying to use two nested for loops to process some files and then create a new file using both variables in the output file name. I have several files in this naming style: S1_L3_all_R1.fastq S1_L3_all_R2.fastq S1_L4_all_R1.fastq S1_L4_all_R2.fastq . . S1_L8_all_R1.fastq... (3 Replies)
Discussion started by: aminards
3 Replies

4. Shell Programming and Scripting

while loops and variables under bash

Hi, This is probably going to be very simple but i came across something i can't quite explain. Here is the situation: i have a list of files, which i'd like to process one by one (get the size, make some tests, whatever) and generate some statistics using different variables. Something... (5 Replies)
Discussion started by: m69w
5 Replies

5. Shell Programming and Scripting

[ask] about unset variables

I'm wondering, is the number of variables will affect execution time of my bash script or maybe affect the cpu workload, cpu memory, etc ? If I create so many variables, should I unset each one of that variables after I used them or after I think they are no longer needed? and if my script... (2 Replies)
Discussion started by: 14th
2 Replies

6. Shell Programming and Scripting

For loops with multiple variables

Hi script gurus. I have need to know how to use for loop with multiple variable. Basically lets take for example /etc/passwd file has following entries The above cat command will basically first greps the real users that have email addresses then converts ':' to '+' then using cut... (4 Replies)
Discussion started by: sparcguy
4 Replies

7. Shell Programming and Scripting

How to check exit status of unset variables

Hi All, Is there any way to check exit status of unset variables? In the following code PathX is not set and the script terminates without checking exit status. #!/bin/bash Path="/tmp/log" cd ${PathX:?} if ;then echo "Exit Status : non zero" else echo "Exit Status :... (2 Replies)
Discussion started by: sussus2326
2 Replies

8. Shell Programming and Scripting

How to unset all variables in shell?

can I use unset to unset all the variables in a shell sciprt? VAR1=1 VAR2=2 VAR3=3 unset whether this unset will afftect any system variables? Thanks, (3 Replies)
Discussion started by: balamv
3 Replies

9. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

10. UNIX for Dummies Questions & Answers

Variables being worked on inside of loops

I have a while read loop that reads values inside of a file and then performs an expr operation on each. Everything works fine, the way it's supposed to but, once the loop is finished, I can not access the variable that I used inside of the loop (but that variable was created outside of the... (7 Replies)
Discussion started by: yongho
7 Replies
Login or Register to Ask a Question