Killing processes in scripts

I have a small problem. It's annoying though. I wrote this shell script:

#
# This script will accept two arguments. The first is a flag and the
# second is a time interval. The only valid flag is '-t' which means
# the user will specify the interval in seconds, otherwise the 
# default is 600 seconds. While the interval is counting, the script
# will run the ps command, cut out the process numbers, and
# place them in a temp file. When the interval is met the script 
# calls another script which uses the temp file and outputs
# the current time, the number of users logged on, and the
# number of processes run during the interval. It will continually
# loop until the script is killed by ^Z.

while [ 1 -eq 1 ]; do

  if test {$1}

  then if [ "$1" = "-t" ]
          then sleep $2; sh colfile &
                let timec=0
                  while [ timec -le $2 ]
                  do
                    ps | cut -f2 -d" " >> proc.tmp
                    let timec=timec+1
                    sleep 1
                  done
           else exit
       fi

  else sleep 5; sh colfile &
       let timec=0
          while [ timec -le 600 ]
          do
            ps | cut -f2 -d" " >> proc.tmp
            let timec=timec+1
            sleep 1
          done
  fi

done

My problem is that when I run it most of the time it will create multiple instances of itself and I have to individually kill each process. I don't know if it is because I don't have an end to the program and have to press ^Z to stop the program or what it is.

When I typed the command:

sh collect -t 2

After stopping it I did a ps and got this:

   PID    TTY  TIME CMD
 29372  pts/4  0:00 sh collect -t 2
 33282  pts/4  0:01 -ksh
 37414  pts/4  0:00 ps
 41604  pts/4  0:00 sh collect -t 2
 46194  pts/4  0:00 sh collect -t 2

Notice the 3 instances it created? Does anyone know why it is doing that and how I can stop it or kill the processes right in my script?

Thanks

Can you post the output of ps -f ? This will show if those other processes are children of the one you started.

Your script does not start other processes running your collect script - what does the colfile script do? Does it start collect jobs?

Hi

Your program contenting three loops, the first one is always true, and second, third depends on the conditions, however, there are 3 process running in your program as the OS treated... ^Z only suspend these 3 process on the backgroud, didn't stop them, advise you stop ur program by ^C...

Your program haven't test yet, but quiet challengable:D

I tried the ^c instead of ^z and it did kill the process. But for some reason it isn't creating the multiple processes now. It is probably because I am interfacing directly with the server on it's own network instead of from home with that glitchy terminal emulator I am using. Whatever the reason thanks for the help.

BTW, I used the first loop that is always true because I didn't know a better way of having it run indefinitely in the background. I am going to incorporate a kill time into it later today or tomorrow to specify the time to run.

Thanks