Subroutines; am I capturing them correctly?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Subroutines; am I capturing them correctly?
# 1  
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!
# 2  
Old 09-22-2016
Without fully understanding the logic you're trying to implement, some comments on the first script, which seems to do what I think you want to be done:
- the exit 1 should come before the fi in all the else branches (unless you want the script to unconditionally exit with error code and never reach further commands).
- you might want to exit with the error code encountered when your command failed.
- the trap command should be executed early in the script, e.g. right after the exec redirection command.
# 3  
Old 09-22-2016
Rudi -

Thank you for the follow up! SO youre saying putting the Exit 1 before the fi in the FIRST else will allow the script to proceed to the "Terminate Hung Services" section? That will allow the script to proceed after provided it's successful, right?

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 ---------------------------------------------------------

else
  echo -------------------------------------------------------
  echo "Essbase Services Stopped Unsuccessfully!"           
  echo -------------------------------------------------------
  exit 1
  fi
  
  
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

Then after the grep section, I want the script to immediately exit after that execution whether its successful or not. So I think I have it correct there?

Also, why the trap portion early on? Wont that exit the script prematurely? Or would I remove the EXIT 0 portion?

Thank you!
# 4  
Old 09-22-2016
1) Yes
2) Yes and No. Why should it exit with an error code 1 if the kills were successful?
3) The trap builtin doesn't execute anything (nor exits) but sort of "sets a pointer" where to jump and execute when the respective signal (here: EXIT) is received/intercepted. Please c.f. man bash (in fact, EXIT 0 specifies the EXIT sigspec twice...)
# 5  
Old 09-22-2016
Quote:
Originally Posted by RudiC
1) Yes
2) Yes and No. Why should it exit with an error code 1 if the kills were successful?
3) The trap builtin doesn't execute anything (nor exits) but sort of "sets a pointer" where to jump and execute when the respective signal (here: EXIT) is received/intercepted. Please c.f. man bash (in fact, EXIT 0 specifies the EXIT sigspec twice...)
Rudi,

You're right, I should exit 0 with either branch of the last if following grep. As either are essentially successes. No need to exit 1 there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question