0

I am writing a GO script to check whether Mongo server is running. My code is as,

import "bytes"
import "os/exec"
import "fmt"

func main() {
    cmd := exec.Command("ps", "-ef", "|", "grep", "mongod", "|", "grep", "-v", "grep", "|", "wc", "-l", "|", "tr", "-d", "'", "'")

    fmt.Println(cmd)
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err := cmd.Run()
    if err != nil {
        fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
        return
    }
    fmt.Println("Result: " + out.String())
}

But Getting Error as , "exit status 1: error: garbage option" . Is there other way to check this with GOLANG? Please let me know.

Priyanka
  • 354
  • 7
  • 14
  • Here you find an start_stop_restart skript for mongodb with bash. There is also an very short command :) https://github.com/patrick0585/Mongo_Start_Stop/blob/master/mongo_start_stop.sh Your command can be ps -ef | awk /[m]ongodb/ – FrankTheTank_12345 May 31 '17 at 11:49
  • And maybe this post will help you how to check with golang how process is running. https://stackoverflow.com/questions/15204162/check-if-a-process-exists-in-go-way – FrankTheTank_12345 May 31 '17 at 11:49
  • @Patrick85 But how to get that PID with go script as it still returns same error with short command as well.. – Priyanka May 31 '17 at 12:00
  • 1
    You can not pipe several commands like that. Please refer to [*How to pipe several commands in Go?*](https://stackoverflow.com/questions/10781516/how-to-pipe-several-commands-in-go) for correct answer. – putu May 31 '17 at 12:04
  • @putu Yes you're right. Your link is great. – FrankTheTank_12345 May 31 '17 at 12:06
  • @Patrick85 Thanks to you both. Your comments helps me a lot. :) – Priyanka May 31 '17 at 12:18
  • That's not a good-practice approach even in bash. Much better to integrate with your init system -- for instance, `exec.Command("systemctl", "is-active", "mongod")` (adjusted for actual service name and init system) is both simpler (only need to check the exit status, not parse output at all) and more robust (can't be thrown off by other programs with similar names -- `vim mongod.conf` matches `grep mongod`, after all); can't be spoofed by non-root users; etc. – Charles Duffy May 31 '17 at 13:32
  • @Patrick85, that `ps | awk` approach has almost all the same bugs -- it'll still treat an existing process `vim mongod.conf` as a match -- and it doesn't tell the OP how to use a pipeline in a process started from Go (which is their most immediate issue). – Charles Duffy May 31 '17 at 13:36

1 Answers1

2

If you want to go beyond porting a bash script to Go (which is often more trouble than it's worth), you can use the mgo library to actually connect to a MongoDB instance and check if it is healthy:

package main

import (
    "gopkg.in/mgo.v2"
    "fmt"
    "os"
)

func main() {
    sess, err := mgo.Dial("localhost")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer sess.Close()
    err = sess.Ping()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("MongoDB server is healthy.")
    os.Exit(0)
}
Adrian
  • 42,911
  • 6
  • 107
  • 99