- go get github.com/mitchellh/go-ps
- go get github.com/go-martini/martini
- go run app.go
Last active
August 29, 2015 14:09
-
-
Save peter-mcconnell/ceb0358e017497bc0e6f to your computer and use it in GitHub Desktop.
mini web server / chrome process killer
This file contains hidden or 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 ( | |
"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