Help with function - is placed in script correctly?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with function - is placed in script correctly?
# 1  
Old 11-15-2016
Help with function - is placed in script correctly?

Hi Folks -

I hope everyone is well.

I just need some assistance with a script I've put together. I haven't used functions before so I was wondering if my script is built properly in terms of architecture?

The first command of the script shuts down services. Then, the second part finds a service called ESSSVR and kills that if it's still up.

Is how I have it set up correct?

Thank you!

Code:
#:: Begin Script Processing --::#
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 all hung ESSSVR Processes"                               
echo ---------------------------------------------------------

function trim {

        arg="$*"
        shopt -s extglob
        arg="${arg#*( )}"
        arg="${arg%*( )}"
        echo "$arg"

}

function kill_soft_then_hard {

        PIDS="$*"

        PID_REGEX_PATTERN="^\s*("
        for pid in $PIDS;do
                PID_REGEX_PATTERN="$PID_REGEX_PATTERN|$pid"
        done
        PID_REGEX_PATTERN="$PID_REGEX_PATTERN)"

        # Here's the chain of signals being sent, when the processes refuse to terminate
        for SIGNAL in 15 1 3 7 9 ;do 
                kill -$SIGNAL $PIDS &>/dev/null
                for((w=1;$w<=30;w++)) ; do
                  if ps ax | grep -qE $PID_REGEX_PATTERN ; then
                          continue
                  else
                          break 2
                  fi
                  sleep 1
                done
        done

}

function kill_by_pattern {

        pattern="$1"
        PIDS="$(ps ax | grep "$pattern" | grep -v grep | awk '{print $1}')"
        PIDS="$(trim $PIDS)"

        [ -n "$PIDS" ] && kill_soft_then_hard $PIDS

}

kill_by_pattern ESSSVR

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Terminated all hung ESSSVR Processes Successfully!"                           
  echo ---------------------------------------------------------
  #::-- If empty, delete YYYY_MMDD error file subdirectory --::#
  trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EP}" EXIT 0
  
else
  echo -------------------------------------------------------
  echo "Terminated all hung ESSSVR Processes Unsuccessfully!"                       
  echo ------------------------------------------------------
  fi
  exit 1

# 2  
Old 11-15-2016
Place seems reasonable to me. Yes.

A question - as a sort of rule of thumb, you usually want to source scripts that define thing like functions, variables, environment variables - you seem to have sourced something which affects the system. Kind of the opposite. Or at least it does important things inside your current code.

A reason to consider: the more external dependencies you have, the more difficult maintenance and debugging become. The sourced script can have all sorts of effects on your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why does this awk script not work correctly?

I have a large database with English on the left hand side and Indic words on the left hand. It so happens that since the Indic words have been entered by hand, there are duplicates in the entries. The structure is as under: English headword=Indic gloss,Indic gloss A small sample will... (6 Replies)
Discussion started by: gimley
6 Replies

2. Shell Programming and Scripting

Shell script worked correctly until I added variable :(

Hi everyone, I have been using a shell script for the last 6 months to copy a database from a POS system, then analyse the database and print the current sales total. This has worked flawlessly, the only issue was that I had hard coded the IP address of the POS and occasionally I would need to... (23 Replies)
Discussion started by: gjws
23 Replies

3. Shell Programming and Scripting

Script runs manually but not correctly from crontab

Hello all, I'm new here and have a question if you don't mind helping me. I have a script that will work if I kick if off manually but not from Cron. My cron entry is this: 05,20,35,50 * * * * /scripts/status.sh > /dev/null 2>&1 The first script (works fine) is this: #!/bin/sh # #... (14 Replies)
Discussion started by: hs3082
14 Replies

4. Shell Programming and Scripting

Script runs manually but not correctly from crontab

Hi all I have this inside a shell script (bash): cd DIRECTORY find . -maxdepth 1 | sed 's#./##' | /usr/bin/xargs -I '{}' chown -Rv '{}' /DIRECTORY/'{}' All the directories in this location are named after usernames, so it simply sets the owner to that of the username of the folder. It... (5 Replies)
Discussion started by: fakesy
5 Replies

5. Shell Programming and Scripting

Perl Script Not Reading Input Files Correctly

This is one of the strangest things that's happening to me. I'm writing a new Perl script that is trying to read a file. The file is originally in .mof format, but I also saved the contents into a .txt file. As a simple test, I wrote this: #!/user/bin/perl -w use strict; ... (3 Replies)
Discussion started by: kooshi
3 Replies

6. Shell Programming and Scripting

script not displaying output correctly

Hi, I am having an issue with my script, ofcourse... I am trying to run commands against a remote server, I am pulling the hostnames or IPs from a file list, then looping thru and running the date cmd. I will be running different cmds just trying to get it working first. The ouput isn't... (2 Replies)
Discussion started by: dfezz1
2 Replies

7. Shell Programming and Scripting

Shell script not unzipping and copying correctly

Hi, I am trying to unzip a file( $pfile, it contains a couple of files and 4 folders with subfolders and files) and have its contents go into a directory instead of into a folder in that directory (ZZZZ), I have the following script: #Unzip the build unzip -o "$HOME/ZZZZ/$pfile" -d... (2 Replies)
Discussion started by: SlumberMachine
2 Replies

8. Shell Programming and Scripting

Find cmd not working correctly in script

I am trying to copy 2 types of files so I can archive them. I tested with a set of commands: touch -t $(date -d "-60 day" +%Y%m%d) WORKDIR/REF find TARGETDIR/ -type f -maxdepth 1 -iname \*.out\* -or -iname \*.log\* ! -newer WORKDIR/REF -exec ls -l {} \; This correctly lists any files in the... (2 Replies)
Discussion started by: prismtx
2 Replies

9. Shell Programming and Scripting

Script not running correctly

Hi all, My script below was working fine till I changed the LOGFILE path (which originally points to a file within the same folder of the script for testing) Only the event.log file path is changed, the rest are still within the current folder. Before the change, running the script works.... (1 Reply)
Discussion started by: phixsius
1 Replies

10. UNIX for Dummies Questions & Answers

Script not working correctly

I have a simple script that I want to run every 30 minutes but only when I execute it. I don't want it to be a crontab job. so i have for example date ls -l who sleep 1800 The first time it executes correctly but after the first time it nevers execute back again. It should execute after... (2 Replies)
Discussion started by: elchalateco
2 Replies
Login or Register to Ask a Question