#! /bin/sh
###############################################################################
#                                                                             #
#                          Network Job Control (NJC)                          #
#                                                                             #
#                                 R M J O B                                   #
#                                   rmjob                                     #
#                                                                             #
*                   Copyright (c) Joerg-R. Hill, April 1992                   *
#                                                                             #
#-----------------------------------------------------------------------------#
# This shell script is called by the submit command, don't use it standalone. #
# If you want to perform some actions when a running job was killed by        #
# submit -r add these at the end of this shell script.                        #
###############################################################################

prgname=`basename $0`
if [ "$1" = "" -o $# -ne 1 ]
then
  echo "$prgname: ERROR: Usage: $prgname <PID>"
  exit
fi

#trap 0 ""

pids=`ps -ef | awk '{print $2}' | grep -v PID`
ppids=`ps -ef | awk '{print $3}' | grep -v PPID`

search=$1
liste=""

while true
do
  found=n
  j=1
  for i in $ppids
  do
    if [ "$i" = "$search" ]
    then
      found=y
      break
    fi
    j=`expr $j + 1`
  done
  if [ "$found" = "n" ]
  then
    break
  fi

  search=`echo $pids | cut -d" " -f$j`
  liste="$liste $search"
done

kill $liste

########################################################
# you can add hereafter your own commands if you want  #
# to perform some actions when a running job is killed #
# ($liste contains a list of PIDs)                     #
########################################################
