How to execute linux command from rakefile?

依然范特西╮ 提交于 2021-01-28 21:29:31

问题


I have the following command:

ps -ef | awk '{if( $8~"java" || $8~"ruby" || $8~"god"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}};'

How do i excute this linux command from rakeFile.

I tried:

task :kill_process do
  `ps -ef | awk '{if( $8~"java" || $8~"ruby" || $8~"god"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}};'`
end

But on executing it's giving error:

awk: cmd. line:1: {if( $8~"java" || $8~"glassfish" || $8~"ruby" || $8~"god" || $8~"couch"){printf("Killing : %s 
awk: cmd. line:1:                                                                                 ^ unterminated string
awk: cmd. line:1: {if( $8~"java" || $8~"glassfish" || $8~"ruby" || $8~"god" || $8~"couch"){printf("Killing : %s 
awk: cmd. line:1:                                                                                 ^ syntax error

I got the solution for my problem: Thanks to @Yurii Verbytskyi

task :kill_process do
   system %q(ps -ef | awk '{if( $8~"java" || $8~"ruby" || $8~"god"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}}') 
end


回答1:


Try this one.

task :kill_process do
   system("ps -ef | awk '{if( $8~\"java\" || $8~\"ruby\" || $8~\"god\"){printf(\"Killing : %s \\n\",$2);{system(\"kill -9 \"$2)};}};'")
end

We need to escape special chars like \n



来源:https://stackoverflow.com/questions/62464476/how-to-execute-linux-command-from-rakefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!