How to kil unix process using gradle task?


(Muthu Selvan SR) #1

I want execute the below unix comand to kill the process

COMMAND : ps -ef | grep "PROCESS_NAME " | xargs kill -9

when i am trying to execute the below gradle task its not running

task Execute(type: Exec) {

commandLine “ps”, “auwx” , “|” , “grep” ,"PROCESS_NAME "

}

its throwing error :

ps: illegal argument: | usage: ps [-AaCcEefhjlMmrSTvwXx] [-O fmt | -o fmt] [-G gid[,gid…]]

[-u]

[-p pid[,pid…]] [-t tty[,tty…]] [-U user[,user…]]

ps [-L]

Please help me "How to execute the unix command with pipe and kill the unix process with pid ?


(Gary Hale) #2

The problem is that’s not one command, that’s three commands piped together using a shell pipe operator ("|"). When gradle executes the command, he’s not executing a shell, he’s executing ps and | is not a valid argument for ps. You have two options, either put your commands in a shell script, or execute a shell and pass the command string as an argument to that. For example:

task sortedProcList(type: Exec) {
  commandLine "bash", "-c", "ps -ef | sort"
}