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


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

#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

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

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


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
pid_array =
pid_array =[]
pid_array.push [Process.ppid]

p2 … (blah blah blah)

puts pid_array

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 :wink: ((but i refuse to give up and go to bash and .bat))

pid_array.each do |smashme|
Process.kill (“HUP”, ‘smashme’)


     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 !!!