Q:

how to kill a command executed within xterm

I'd like to control the execution of shell commands within an xterm window. 
This is an example code (Ruby):
pid = Process.fork
if pid.nil? 
  exec "xterm -e 'while true; do echo -n .; sleep 1; done'"
else
  puts "pid is: #{pid}"
  Process.detach(pid)
end

However, the xterm window doesn't get killed:
$ ./tst2.rb 
pid is: 26939
$ ps aux | grep xterm | grep -v grep
user   26939  0.0  0.0   4508   800 pts/3    S    13:11   0:00 sh -c xterm -e 'while true; do echo -n .; sleep 1; done'
user   26943  0.6  0.0  72568  6280 pts/3    S    13:11   0:00 xterm -e while true; do echo -n .; sleep 1; done
$ kill 26939
$ ps aux | grep xterm | grep -v grep
user   26943  0.1  0.0  72568  6280 pts/3    S    13:11   0:00 xterm -e while true; do echo -n .; sleep 1; done

How can the xterm window be killed?

A:

You need to find the PID of the xterm process in order to kill it. In your case, the easiest way would be to bypass the sh process, as you don't need it. To do this, you have to pass the arguments to exec in an array, not as a command line:
exec('xterm', '-e', 'while true; do echo -n .; sleep 1; done')

