Skip to content

Instantly share code, notes, and snippets.

@peter-mcconnell
Last active August 29, 2015 14:09
Show Gist options
  • Save peter-mcconnell/ceb0358e017497bc0e6f to your computer and use it in GitHub Desktop.
Save peter-mcconnell/ceb0358e017497bc0e6f to your computer and use it in GitHub Desktop.
mini web server / chrome process killer
  • go get github.com/mitchellh/go-ps
  • go get github.com/go-martini/martini
  • go run app.go
package main
import (
"github.com/mitchellh/go-ps"
"log"
"net/http"
"os"
)
const listenUrl string = ":6732"
func main() {
http.HandleFunc("/chrome", handler)
log.Println("Listening ...")
err := http.ListenAndServe(listenUrl, nil)
if err != nil {
log.Fatalln("ListenAndServe:", err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
killChrome()
w.Write([]byte("OK"))
}
func killChrome() {
log.Println("killing chrome processes")
prcs, _ := ps.Processes()
for _, p := range prcs {
if p.Executable() == "chrome.exe" {
pc, findErr := os.FindProcess(p.Pid())
if findErr != nil {
log.Fatalln(findErr)
}
killErr := pc.Kill()
if killErr != nil {
log.Fatalln(killErr)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment