Sponsored Content
Top Forums Shell Programming and Scripting Subroutines; am I capturing them correctly? Post 302981971 by SIMMS7400 on Thursday 22nd of September 2016 03:31:53 AM
Old 09-22-2016
Subroutines; am I capturing them correctly?

HI Folks -

Again, I'm very sorry for the amateur post. I'm reletively new to shell scripting so please bear with me.

I have a script that execute another script which stops a particular set of services for my application. IF the execution is successful, I want to check for any hung ESSSVR processes.

Am I capturing the flow correctly here? Normally in DOS wed just use a GOTO label.

Here is my code:

Code:
#:: SET SCRIPT NAME
_SN=STOP_ESSBASE_SVCS

#:: SET PATHS
_MAINPATH=/home/essadmin/Hyperion_Batch/
_LOGPATH=Logs/
_ERRORPATH=Errors/

#:: SET DATE & TIME VARIBLES:
_DAY=$(date +%d)
_MONTH=$(date +%m)
_YEAR=$(date +%Y)
_DATESTAMP=${_YEAR}${_MONTH}${_DAY}
_HOUR=$(date +%H)
_MINUTE=$(date +%M)
_SECOND=$(date +%S)
_TIME=${_HOUR}${_MINUTE}
_DATETIMESTAMP=$_DATESTAMP-$_TIME

#:: Prepare Log and Error File Directories
_ARC_LF=${_MAINPATH}${_LOGPATH}${_YEAR}${_MONTH}
_ARC_EF=${_MAINPATH}${_ERRORPATH}${_YEAR}${_MONTH}
    mkdir -p ${_ARC_LF}
    mkdir -p ${_ARC_EF}

#:: Prepare Filename Format    
_FN=${_DATESTAMP}_${_SN}

#:: Prepare STDOUT & STDERR 
_LF=${_ARC_LF}/${_FN}.log
_EF=${_ARC_EF}/${_FN}.err

exec 2>${_EF} > ${_LF}

#:: Execute script commands
echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                           
echo .                                                                                                        
echo "Stop Essbase Services"                                         
echo ---------------------------------------------------------

./home/essadmin/scripts/shutdown.sh

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Essbase Services Stopped Successfully!"                 
  echo ---------------------------------------------------------

else
  echo -------------------------------------------------------
  echo "Essbase Services Stopped Unsuccessfully!"           
  echo -------------------------------------------------------
  fi
  exit 1
  
echo ---------------------------------------------------------
echo "Terminate Hung Services"                               
echo ---------------------------------------------------------
_SERVICE=ESSSVR
  
ps -ef | grep ${_SERVICE} | awk '{print $2}' | xargs kill -9

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Essbase Services Stopped Successfully!"                           
  echo ---------------------------------------------------------
  trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EF}" EXIT 0
  
else
  echo -------------------------------------------------------
  echo "Essbase Services Stopped Unsuccessfully!"                       
  echo ------------------------------------------------------
  fi
  exit 1

Essentially, if the success code is zero, will it jump over the else to proceed to the next execution? Or do I need to inslude the ps -ef portion in the first If/then branch?

Like this:

Code:
#:: SET SCRIPT NAME
_SN=STOP_ESSBASE_SVCS

#:: SET PATHS
_MAINPATH=/home/essadmin/Hyperion_Batch/
_LOGPATH=Logs/
_ERRORPATH=Errors/

#:: SET DATE & TIME VARIBLES:
_DAY=$(date +%d)
_MONTH=$(date +%m)
_YEAR=$(date +%Y)
_DATESTAMP=${_YEAR}${_MONTH}${_DAY}
_HOUR=$(date +%H)
_MINUTE=$(date +%M)
_SECOND=$(date +%S)
_TIME=${_HOUR}${_MINUTE}
_DATETIMESTAMP=$_DATESTAMP-$_TIME

#:: Prepare Log and Error File Directories
_ARC_LF=${_MAINPATH}${_LOGPATH}${_YEAR}${_MONTH}
_ARC_EF=${_MAINPATH}${_ERRORPATH}${_YEAR}${_MONTH}
    mkdir -p ${_ARC_LF}
    mkdir -p ${_ARC_EF}

#:: Prepare Filename Format    
_FN=${_DATESTAMP}_${_SN}

#:: Prepare STDOUT & STDERR 
_LF=${_ARC_LF}/${_FN}.log
_EF=${_ARC_EF}/${_FN}.err

exec 2>${_EF} > ${_LF}

#:: Execute script commands
echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                           
echo .                                                                                                        
echo "Stop Essbase Services"                                         
echo ---------------------------------------------------------

./home/essadmin/scripts/shutdown.sh

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Essbase Services Stopped Successfully!"                 
  echo ---------------------------------------------------------
    echo -------------------------------------------------------
    echo "Terminate Hung Services"                               
    echo -------------------------------------------------------
    _SERVICE=ESSSVR
  
    ps -ef | grep ${_SERVICE} | awk '{print $2}' | xargs kill -9

    if [ $? -eq 0 ]
    then
    echo -------------------------------------------------------
    echo "Service Terminated Successfully!"                           
    echo -------------------------------------------------------
    trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EF}" EXIT 0
  
    else
    echo ------------------------------------------------------
    echo "No Service Available to Termininate!"                       
    echo ------------------------------------------------------
    fi
    exit 0
   
else
  echo -------------------------------------------------------
  echo "Essbase Services Stopped Unsuccessfully!"           
  echo -------------------------------------------------------
  fi
  exit 1

Thank you, all!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

shell scripting: calling subroutines

How do I define and call a subroutine in a ksh script (or any shell)? I'm writing a script that uses a lot of the same code over and over and I don't want to waste time/space. Thanks, Chuck (2 Replies)
Discussion started by: 98_1LE
2 Replies

2. UNIX for Dummies Questions & Answers

Passing Hash Tables to Subroutines

Hi: How do I pass a hash table down to a subroutine along with some other variables? For example, I have say a subroutine play_with_hash: sub play_with_hash { my( $var1, $var2, %my_hash ) = @_; #do stuff with %my_hash ........... } Then I want to call the subroutine... (1 Reply)
Discussion started by: mirzabhai
1 Replies

3. HP-UX

HP-UX will not boot correctly

i've same failure too, but this command boot pri isl not work/not found Thanks! (1 Reply)
Discussion started by: pantas manik
1 Replies

4. Shell Programming and Scripting

if not working correctly

Anyone have an idea why this if statement does not work correctly? "test2.sh" 18 lines, 386 characters #!/usr/bin/sh WARNING=80 CRITICAL=95 check_it() { if ] || ];then echo "YES ] || ]" else echo "NO ] || ]" fi } check_it 80.1 check_it 81.1 (3 Replies)
Discussion started by: 2dumb
3 Replies

5. Shell Programming and Scripting

Finding Subroutines

I'm maintaining a variety of programs (mostly in Perl) I didn't build, and the perllib is huge on the server. When I run across a user-defined subroutine it takes me at least an hour to look through all the logical places, and sometimes I still can't find the definition. Is there an easy way to... (2 Replies)
Discussion started by: pdownes
2 Replies

6. Shell Programming and Scripting

Escaping ** correctly

Hello This should be easy, but bash is giving me headaches. At the command line the following command works: duplicity --include /home --exclude '**' / file:///foo Doing that from a script is not straightforward. Note that it is basically a requirement that I place the... (3 Replies)
Discussion started by: brsett
3 Replies

7. Shell Programming and Scripting

How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone, I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from command line like CASE:1 ( Without an explict... (2 Replies)
Discussion started by: ennstate
2 Replies

8. Homework & Coursework Questions

Help with perl subroutines

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This subroutine needs to check if there was a file name given on the command line. If so, return that. Otherwise... (1 Reply)
Discussion started by: yonkers062986
1 Replies

9. Shell Programming and Scripting

How can i use switches type arguments for subroutines in perl

i want to call subroutines in perl like: sub temp { ---- some code ----- } temp(-switchName, value1, --switchName2, value2) Like i know getoptions::Long is there for command line switches type arguments. So i want to know for subroutine type arguments. (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

10. Shell Programming and Scripting

how to obtain a variable between subroutines

#!/usr/bin/bash sub1 () { for ((i=0;i<10;i++)) do export a=$i; echo "value of a is $a"; sleep 1 done } sub1 & sub2 () { for ((j=0;j<10;j++)) do echo "value of a is $a"; sleep 1 done } (5 Replies)
Discussion started by: Arun_Linux
5 Replies
All times are GMT -4. The time now is 06:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy