2017-01-09 19 views
0

我想在xterm窗口內控制shell命令的執行。 這是一個示例代碼(紅寶石):如何殺死在xterm內執行的命令

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 

然而,xterm窗口不被打死:

$ ./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 

如何xterm窗口會死嗎?

回答

1

您需要找到xterm進程的PID以殺死它。在你的情況下,最簡單的方法是繞過sh進程,因爲你不需要它。爲此,您必須將參數傳遞給exec而不是命令行:

exec('xterm', '-e', 'while true; do echo -n .; sleep 1; done')