How to execute linux command from rakefile?

237 Views Asked by At

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

There are 1 best solutions below

2
lokanadham100 On

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