I am trying to write the output of top to a file every 5 seconds which I also got to work.
cmd = "top -s 5 -pid 123 >> top.txt"
p = IO.popen(cmd)
However, the problem I have is that I can't find a way of closing top
I have tried
Process.kill('KILL', p.pid)
but top keeps writing to the output file.
p.close hangs but if I ctrl + c it does seem to exit the top command as well. But this as this requires me to manually ctrl + c it is not a viable solution.
Any help would be appreciated!