Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created September 23, 2013 13:12
Show Gist options
  • Save kwmt/6670208 to your computer and use it in GitHub Desktop.
Save kwmt/6670208 to your computer and use it in GitHub Desktop.
指定したプロセスのPIDを検索して、そのPIDをKILLする。
package main
import (
"fmt"
"log"
"os/exec"
"os"
"bytes"
"strconv"
)
func main() {
var pid int
//検索したいプロセス
foo := "./foo"
// lsof コマンドでプロセスIDを検索
out, err := exec.Command("lsof", foo).Output()
if err != nil {
log.Fatal(err)
}
// プロセスIDを検索(出力)
outsplit := bytes.Split(out, []byte(" "))
for _, o := range outsplit {
pid, err = strconv.Atoi(string(o))
if err == nil {
fmt.Printf("%d\n", pid)
break;
}
}
// プロセスをKILL
process, err := os.FindProcess(pid)
if err != nil {
log.Fatal(err)
}
err = process.Kill()
if err != nil {
log.Fatal(err)
}
fmt.Println("プロセス",pid,"をKILLしました")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment