Utilities not dying after script run

Hi folks,

Friendly router geek wanting to be a programmer here...

So I worked with another guy here and came up with this to capture Unix admin data:

#!/bin/ksh
#
#
# Set Default Paths
#
PATH=/usr/apps/client/bin:$PATH; export PATH
LD_LIBRARY_PATH=/usr/apps/client/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
NOSHOME=/usr/apps/client/bin; export NOSHOME

# Execute the NOS provided top program with CPU MEMORY and LOAD values to be extracted to temporary files
#
$NOSHOME/top -d1 -q | /usr/bin/head -n 5 | /usr/bin/grep CPU | /usr/bin/awk -F"," '{print $1}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/awk -F" " '{print $1}' > /tmp/cpu.out
$NOSHOME/top -d1 -q | /usr/bin/head -n 5 | /usr/bin/grep Memory | /usr/bin/awk -F"," '{print $1,$2}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/awk -F" " '{print $1","$4}' > /tmp/mem.out
$NOSHOME/top -d1 -q | /usr/bin/head -n 5 | /usr/bin/grep Memory | /usr/bin/awk -F"," '{print $3,$4}' | /usr/bin/awk -F" " '{print $1","$4}' > /tmp/swap.out
$NOSHOME/top -d1 -q | /usr/bin/head -n 5 | /usr/bin/grep load | /usr/bin/awk -F";" '{print $2}' | /usr/bin/awk -F":" '{print $2}' | /usr/bin/sed 's/^[       ]*//;s/[        ]*$//' > /tmp/loadavg.out

# Gather all data into single file and clean up
#
CPU=`cat /tmp/cpu.out`
MEM=`cat /tmp/mem.out`
SWAP=`cat /tmp/swap.out`
LOAD=`cat /tmp/loadavg.out`
HOST=`/usr/bin/hostname`
DAY=`/usr/bin/date +%b-%d-%y`
TIME=`/usr/bin/date +%H:%M:%S`

# Configure date values to figure out proper storage of comma delimited values
#
typeset -i MONTH=`/usr/bin/date +%m`
MONTH=$(echo "$MONTH" | tr ' ')
typeset -i DAY=`/usr/bin/date +%d`
DAY=$(echo "$DAY" | tr ' ')
typeset -i YEAR=`/usr/bin/date +%Y`
YEAR=$(echo "$YEAR" | tr ' ')

# Create a variable FILE to concat variables into a single variable to test against
FILE=$MONTH"-"$YEAR-$HOST".dat"

# Check to see if file is empty. If not, populate the values, otherwise create the needed file and populate
#
if [ -e $NOSHOME/../data/OSKPI/$FILE ]
  then
    echo $HOST","$DAY","$TIME","$CPU","$MEM","$SWAP","$LOAD >> $NOSHOME/../data/OSKPI/$MONTH-$YEAR-$HOST.dat
  else
    echo $HOST","$DAY","$TIME","$CPU","$MEM","$SWAP","$LOAD > $NOSHOME/../data/OSKPI/$MONTH-$YEAR-$HOST.dat
  fi

# Remove all temporary files and exit
#
rm /tmp/cpu.out /tmp/mem.out /tmp/swap.out /tmp/loadavg.out
exit 0

But once the script runs, it leaves processes still running as shown:

bash-3.00$ ps -efa | grep -v wrapper | grep -v oracle | grep -v java 
       .
         .
  
       .
      root 11153 11098   0 07:00:04 ?           0:00 /usr/bin/sed s/^[       ]*//;s/[        ]*$//
      root 19606   479   0 00:14:49 ?           0:00 /usr/lib/ssh/sshd
      root 11150 11098   0 07:00:04 ?           0:00 /usr/bin/grep load
      root 11149 11098   0 07:00:04 ?           0:00 /usr/bin/head -n 5
      root 11098 11097   0 07:00:00 ?           0:00 /bin/ksh /usr/apps/client/bin/gatherOSKPI.sh
      root 11152 11098   0 07:00:04 ?           0:00 /usr/bin/awk -F: {print $2}
      root 11097   287   0 07:00:00 ?           0:00 sh -c /usr/apps/client/bin/gatherOSKPI.sh
      root 11146 11098  50 07:00:04 ?        1040:23 /usr/apps/client/bin/top -d1 -q
      root 11151 11098   0 07:00:04 ?           0:00 /usr/bin/awk -F; {print $2}
  -bash-3.00$
  

Can anyone explain why this is?

Thanks in advance

Well knowing what OS you are running may help a bit...
the top I see is the standard top utility?
Why initialize LD_LIBRARY since you are not using it ( I may be wrong but I see no program here that needs it... or I missed it... Ah and in ksh you can export straightaway VARIABLES export VAR=value ...)

1 Like

Sorry for not giving the OS, I am running Solaris 2.10

but i think I have found the issue.

The top command I am using was suggested by another tech.
In taking it apart, I found that the "-d" flag sets a refresh rate. but I only want the instance of top to run once per command line in the script. I don't want it to refresh.

So I changed the "-d1" to "-n1" as "-n" controls the number of iterations top carries out.

I am now testing my solution.

Thanks for your response!

Marc

You might want to capture your 'top' output once in a file or env var, so all the commands get data from the same moment.

1 Like

that is a great idea for improving the efficiency of the script, and I am looking into tweaking it to make it better now I've got it working. My boss has approved it for beta deployment in our lab and I am pleased at what I'm learning :smiley:

Thank you folks for your help.