Skip to content

Commit 5464e85

Browse files
committed
Kill the browser and restart it in linux
1 parent 1b8d7eb commit 5464e85

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

killbrowser.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
func killBrowserHandler(c *gin.Context) {
11+
12+
var data struct {
13+
Action string `json:"action"`
14+
Process string `json:"process"`
15+
URL string `json:"url"`
16+
}
17+
18+
c.BindJSON(&data)
19+
20+
command, err := findBrowser(data.Process)
21+
22+
log.Println(command)
23+
24+
if err != nil {
25+
c.JSON(http.StatusInternalServerError, err)
26+
}
27+
28+
if data.Action == "kill" || data.Action == "restart" {
29+
_, err := killBrowser(data.Process)
30+
if err != nil {
31+
c.JSON(http.StatusInternalServerError, err)
32+
}
33+
}
34+
35+
if data.Action == "restart" {
36+
_, err := startBrowser(command, data.URL)
37+
if err != nil {
38+
c.JSON(http.StatusInternalServerError, err)
39+
}
40+
}
41+
42+
}

killbrowser_linux.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
)
7+
8+
func findBrowser(process string) ([]byte, error) {
9+
ps := exec.Command("ps", "-A", "-o", "command")
10+
grep := exec.Command("grep", process)
11+
head := exec.Command("head", "-n", "1")
12+
13+
return pipe_commands(ps, grep, head)
14+
}
15+
16+
func killBrowser(process string) ([]byte, error) {
17+
cmd := exec.Command("pkill", "-9", process)
18+
return cmd.Output()
19+
}
20+
21+
func startBrowser(command []byte, url string) ([]byte, error) {
22+
parts := strings.Split(string(command), " ")
23+
cmd := exec.Command(parts[0], url)
24+
return cmd.Output()
25+
}

0 commit comments

Comments
 (0)