# Fork -\> PID -\> Array -\> Process.kill, help!

**URL:** <https://www.ruby-forum.com/t/fork-pid-array-process-kill-help/142555>\
**Category:** Ruby\
**Created:** [July 9, 2008, 6:19am UTC](https://www.ruby-forum.com/t/fork-pid-array-process-kill-help/142555 "2008-07-09T06:19:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fizzbang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c68b51/32.png) [@fizzbang](https://www.ruby-forum.com/u/fizzbang)\
**Post date:** [July 9, 2008, 6:19am UTC](https://www.ruby-forum.com/t/fork-pid-array-process-kill-help/142555/1 "2008-07-09T06:19:42Z")

</div>

Hi

Complete “noob” to writing my own scripts, I thought I would give Ruby a  
try and I surprised myself with how far i have completed in 1 day.

Alas I seem to have put my brain into a infinite loop with the following  
problem.

#usual blah

#delete files from dirs  
system(‘rm -f /root/stats/\*’)

# unmount and re-mount a NFS filesystem

system(‘umount -f /mnt/somedir’)  
system(‘mount -t nfs -o blah hostname etc mountpoint etc’)

> > > everything is sweet up to this point but “dstat” and “netstat” need to start \>\>\> before “bonnie” and finish after “bonnie”

> > > To do this from the docs it looks like i should “fork” of the stats  
> > > collection threads so they do not block my IO generation process (bonnie++)

# Start capture of statistics

# fork off my 2 stat collectors before

p1 = fork {  
system('dstat --nooutput --outputfile /root/stats/dstat.csv  
2&\>1`)  
}

p2 = fork {  
system(‘netstat 1 | grep ip.addr \>\> /root/stats/netstat.txt’)  
}

```
      system('bonnie++ -options to generate IO' -o

```

/root/stats/bonnie.csv’)

> > > Now this script actually runs and completes with no errors, BUT i get  
> > > orphaned processes from “dstat” and “netstat” because they will run forever \>\>\> until I send them a “signal”

> > > Now i cant send that signal as using “fork” the Process.wait call poll it \>\>\> every second until it completes and it never actually terminates.

> > > So i have changed tack and I am looking for a lower level term of the  
> > > “dstat” and “netstat” by getting the numerical PID and using Process.kill

My current solution

p1 = fork {  
system('dstat --nooutput --outputfile /root/stats/dstat.csv  
2&\>1`)  
pid\_array = Array.new  
pid\_array =[Process.pid]  
pid\_array.push [Process.ppid]  
}

p2 … (blah blah blah)

puts pid\_array  
12345  
67891

> > > This gives me and array that now holds the PID of the “dstat” and “netstat” \>\>\> threads (hooray !!!)

> > > Now the tricky part, I need to give these 2 values to “Process.kill” in a \>\>\> loop so that the function takes each of my array values for the PID and  
> > > kills them in turn.

> > > This is as far as i have got before I ran for the coffee machine and a  
> > > melt-down 😉 ((but i refuse to give up and go to bash and .bat))

pid\_array.each do |smashme|  
Process.kill (“HUP”, ‘smashme’)

or

```
     Process.kill ("HUP", 'pid_array')

```

> > > The error I get back is “invalid argument” for Process.kill

> > > The docs do say that it expects a numerical value for the second variable, I \>\>\> would like it to use the numbers I have managed to obtain with my array.

```
     Process.kill ("HUP", 12345)
     Process.kill ("HUP", 67891)

```

TIA !!!
