From 223949d1d83f5f4104c8bb2599d77423968d6a5c Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 2 Sep 2020 16:57:43 -0700 Subject: [PATCH 1/2] cmd/runmqserver.verifySingleProcess: optimize Instead of using `ps`, which reads three `/proc` files per each process (`stat`, `status`, and `cmdline`), use `pgrep` which only reads cmdline. Also, `pgrep` output is simpler -- it only lists PIDs, one per line, so to find out the number of processes we just need to count newlines. Signed-off-by: Kir Kolyshkin --- cmd/runmqserver/process.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/runmqserver/process.go b/cmd/runmqserver/process.go index dafd9b6c..2b344b5c 100644 --- a/cmd/runmqserver/process.go +++ b/cmd/runmqserver/process.go @@ -43,9 +43,9 @@ func verifySingleProcess() error { // Verifies that there is only one instance running of the given program name. func verifyOnlyOne(programName string) (int, error) { // #nosec G104 - out, _, _ := command.Run("ps", "-e", "--format", "cmd") + out, _, _ := command.Run("pgrep", programName) //if this goes wrong then assume we are the only one - numOfProg := strings.Count(out, programName) + numOfProg := strings.Count(out, "\n") if numOfProg != 1 { return numOfProg, fmt.Errorf("Expected there to be only 1 instance of %s but found %d", programName, numOfProg) } From ade018e06688eee1ca99b597d2fb250ff520ffc4 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 2 Sep 2020 17:12:28 -0700 Subject: [PATCH 2/2] cmd/runmqserver.verifySingleProcess: improve 1. Check command.Run error, print it out (as debug only). 2. Simplify verifyOnlyOne() return values (bool is enough). 3. Add more logging. Signed-off-by: Kir Kolyshkin --- cmd/runmqserver/process.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cmd/runmqserver/process.go b/cmd/runmqserver/process.go index 2b344b5c..7e9f2a6b 100644 --- a/cmd/runmqserver/process.go +++ b/cmd/runmqserver/process.go @@ -32,8 +32,8 @@ func verifySingleProcess() error { } // Verify that there is only one runmqserver - _, err = verifyOnlyOne(programName) - if err != nil { + one := verifyOnlyOne(programName) + if !one { return fmt.Errorf("You cannot run more than one instance of this program") } @@ -41,15 +41,26 @@ func verifySingleProcess() error { } // Verifies that there is only one instance running of the given program name. -func verifyOnlyOne(programName string) (int, error) { +func verifyOnlyOne(programName string) bool { // #nosec G104 - out, _, _ := command.Run("pgrep", programName) - //if this goes wrong then assume we are the only one + out, _, err := command.Run("pgrep", programName) + if err != nil { + // unable to verify: bad, but not fatal + log.Debug(err.Error()) + return true + } numOfProg := strings.Count(out, "\n") - if numOfProg != 1 { - return numOfProg, fmt.Errorf("Expected there to be only 1 instance of %s but found %d", programName, numOfProg) + switch numOfProg { + case 0: + // unable to verify: bad, but not fatal + log.Debugf("can't find %s using pgrep", programName) + return true + case 1: + return true } - return numOfProg, nil + + log.Errorf("Expected to have 1 instance of %s, got %d", programName, numOfProg) + return false } // Determines the name of the currently running executable.