How to start and control and kill a process using Go

I am writing a program in Go that should open a portable browser on a given page and monitor it so that if the browser is closed, it should be automatically opened. Also, the browser should be closed when the program exits.

So far, I managed to open the browser when the user closed it, however I cannot close it.

Here is my code:

package browser

import (
    "fmt"
    "os/exec"

    log "github.com/sirupsen/logrus"
)

type iBrowserHandler interface {
    Start(processListener chan bool) error
    Stop() error
    KillProcess() error
}

type browserHandler struct {
    cmd            *exec.Cmd
    pathToChromium string
    pdvEndpoint    string
}

func newBrowserHandler(pathToChromium string, pdvEndpoint string) iBrowserHandler {
    b := &browserHandler{}
    b.pathToChromium = pathToChromium
    b.pdvEndpoint = pdvEndpoint

    return b
}

func (b *browserHandler) Start(processListener chan bool) error {
    endpoint := fmt.Sprintf("--app=%s", b.pdvEndpoint)

    b.cmd = exec.Command(b.pathToChromium, endpoint, "--kiosk")
    err := b.cmd.Run()
    if err != nil {
        log.WithError(err).Error("Error with the browser process")
        processListener <- false
    } else {
        processListener <- true
    }

    return err
}

func (b *browserHandler) Stop() error {
    err := b.cmd.Process.Release()
    if err != nil {
        log.WithError(err).Fatal("Error shutting down chromium")
    }

    return err
}

func (b *browserHandler) KillProcess() error {
    err := b.cmd.Process.Kill()
    if err != nil {
        log.WithError(err).Fatal("Error killing chromium process")
    }

    return err
}

And this is the function that I use to launch the browser:

var iBrowserHandler handler

func init() {
    var pathToChromium string
    var c = config.GetInstance()
    var os = runtime.GOOS

    if os == "windows" {
        pathToChromium = "chromium-browser\\ChromiumPortable.exe"
    } else {
        pathToChromium = "chrome"
    }

    handler = newBrowserHandler(pathToChromium, c.GetConfig().PDVUrl)
}

func StartBrowser(done chan bool) {
    browserProcessListener := make(chan bool)
    defer close(browserProcessListener)
    go handler.Start(browserProcessListener)

    var tryReopenBrowser = true
    for {
        select {
        case <-browserProcessListener:
            if tryReopenBrowser {
                log.Warn("Browser process is stopped. Attempting to restart")
                go handler.Start(browserProcessListener)
            } else {
                log.Warn("Browser process is stopped. Will not attempt to restart")
            }

        case <-done:
            log.Info("Shutting down browser")
            tryReopenBrowser = false
            handler.KillProcess()
            return

        default:
        }
    }
}

When the program starts, I call the StartBrowser () function. After launching the browser, it continues to listen if it has been closed (the function cmd.Run()blocks the execution of the thread until the process ends).

If the browser was closed, my program tries to open it again.

, . cmd.Process.Release() cmd.Process.Kill(), . ?

+4
2

! , Go cmd.Process.Kill() , , .

Windows, KillProcess() :

func (b *browserHandler) KillProcess() error {
    log.Info("Killing browser process")
    kill := exec.Command("taskkill", "/T", "/F", "/PID", strconv.Itoa(b.cmd.Process.Pid))
    err := kill.Run()
    if err != nil {
        log.WithError(err).Error("Error killing chromium process")
    }
    log.Info("Browser process was killed")

    return err
}

: Windows, Unix.

+1

cmd.Run() cmd.Start() .

0

Source: https://habr.com/ru/post/1688525/


All Articles