Last active
August 29, 2015 14:03
-
-
Save y-abe/d03b3ebf0ff79859732d to your computer and use it in GitHub Desktop.
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 ( | |
| "bytes" | |
| "fmt" | |
| "log" | |
| "os/exec" | |
| "strconv" | |
| ) | |
| // ExecPs returns output of ps aux command | |
| func ExecPs() ([][]byte, error) { | |
| buf, err := exec.Command("ps", "aux").Output() | |
| if err != nil { | |
| return nil, err | |
| } | |
| out := bytes.Split(buf, []byte("\n")) | |
| return out, nil | |
| } | |
| // Filter extracts Atom processes | |
| func Filter(rows [][]byte) [][]byte { | |
| var out [][]byte | |
| for _, buf := range rows { | |
| if bytes.Contains(buf, []byte("/Applications/Atom.app")) { | |
| out = append(out, buf) | |
| } | |
| } | |
| return out | |
| } | |
| // GetPid extracts process ID from ps output | |
| func GetPid(rows [][]byte) ([]int, error) { | |
| out := []int{} | |
| for _, buf := range rows { | |
| buf = bytes.Fields(buf)[1] | |
| sbuf := string(buf) | |
| ibuf, err := strconv.Atoi(sbuf) | |
| if err != nil { | |
| return nil, err | |
| } | |
| out = append(out, ibuf) | |
| } | |
| return out, nil | |
| } | |
| // KillProcess executes kill command for given process id | |
| func KillProcess(pid int) error { | |
| cmd := exec.Command("kill", strconv.Itoa(pid)) | |
| fmt.Println("kill " + strconv.Itoa(pid)) | |
| err := cmd.Run() | |
| return err | |
| } | |
| func main() { | |
| // execute ps aux command | |
| out, err := ExecPs() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // extract the result with Atom process | |
| out = Filter(out) | |
| // get process ID | |
| pids, err := GetPid(out) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // execute kill command for IDs of the Atom processes | |
| for _, pid := range pids { | |
| err = KillProcess(pid) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kill Atom.app processes because often it's unstable.