问题
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