Killing Linux Process by it's name/command in go

Hello, i am converting an bash code to golang code
If i run /usr/bin/getmykey &> /dev/null and i want to kill the process then in bash i will run this :
ps aux | grep getmykey | grep -v grep | awk '{print "kill -9 " $2;system("kill -9 " $2)}'

I don’t want to use bash, i want to use an compiled language and i already written code in go just this part isn’t written.
Is there any simple way to kill the process via Golang ?

Similar to your other conversion problem.

Try to replace string manipulation and filtering with equivalent go code.

This leaves you with 2 times to shell out (to not introduce syscalls yet), one to get the list of processes via ps and another to do the actual kill.

can we just do pidof getmykey
and then get differentiate the string into slices and get first element and then kill -9 element ?

There might or might not be abstractions or libraries available that give you a simple function to call to get a list of pids for a given binary name.

Though shelling out to ps aux, scanning the lines for a given pattern, shelling to kill -p is probably much quicker written than it would take to find libraries doing the thing for you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.