【发布时间】:2020-10-13 09:06:14
【问题描述】:
我有一个 rake 任务:
task :kill_process do
current_process_id = Process.pid
puts current_process_id
ruby_process_command = "ps -ef | awk '{if( $8~" + "ruby && $2!=" + current_process_id.to_s + "){printf(" + "Killing ruby process: %s " + "\\n " + ",$2);{system(" + "kill -9 " + "$2)};}};'"
puts ruby_process_command
system (ruby_process_command)
end
我得到了:
awk: cmd. line:1: {if( $8~ruby && $2!=23699){printf(Killing ruby process: %s \n ,$2);{system(kill -9 $2)};}};
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: {if( $8~ruby && $2!=23699){printf(Killing ruby process: %s \n ,$2);{system(kill -9 $2)};}};
awk: cmd. line:1: ^ backslash not last character on line
有解决办法吗?
我试过了:
ruby_process_command = "ps -ef | awk '{if( $8~" + '"' + "ruby" + '"' + "&& $2!=" + current_process_id.to_s + "){printf(" + '"' + "Killing ruby process: %s " + "\\n" + '"' + ",$2);{system(" + '"' + "kill -9 " + '"' + "$2)};}};'"
这样就可以正常使用了,有没有其他的好方法呢
【问题讨论】: