Created
January 15, 2022 18:47
-
-
Save costa86/51961deeddd91a648fd297def6cb03e3 to your computer and use it in GitHub Desktop.
kill process by name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"syscall" | |
"github.com/shirou/gopsutil/v3/process" | |
) | |
var print = fmt.Println | |
func killProcessByName(processName string) { | |
processes, err := process.Processes() | |
if err != nil { | |
panic(err) | |
} | |
for _, p := range processes { | |
name, err := p.Name() | |
if err != nil { | |
panic(err) | |
} | |
if name == processName { | |
err := p.SendSignal(syscall.SIGINT) | |
if err != nil { | |
panic(err) | |
} | |
print("Killed " + name) | |
break | |
} | |
} | |
} | |
func main() { | |
killProcessByName("agenda") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment