Golang : Check from web if Go application is running or not
When we run some processes in the background to perform certain tasks and it would be a good idea to know if the process is still alive and kicking from time to time.
In this tutorial, we will learn how to check if a Golang program is still running in the memory and also spit out some custom information with a web browser.
In this example, our program will inform the world that it is running fine if everything is ok and reply with the total number of processes running in memory. Please change the code below to suit your requirements.
Run this code below and then point your browser to http://localhost:8088
and http://localhost:8088/getstatus
(case sensitive) to see the output
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os/exec"
)
func Greet(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello! I'm the getstatus.go program and I'm still alive and kicking."))
}
func ReplyStatus(w http.ResponseWriter, r *http.Request) {
// taken from
// https://www.socketloop.com/tutorials/golang-pipe-output-from-one-os-exec-shell-command-to-another-command
first := exec.Command("ps", "-ef")
second := exec.Command("wc", "-l")
reader, writer := io.Pipe()
first.Stdout = writer
second.Stdin = reader
var buff bytes.Buffer
second.Stdout = &buff
first.Start()
second.Start()
first.Wait()
writer.Close()
second.Wait()
total := buff.String() // convert output to string
w.Write([]byte(fmt.Sprintf("Total processes running on this server : %s ", total)))
}
func main() {
// http.Handler
mux := http.NewServeMux()
mux.HandleFunc("/", Greet)
mux.HandleFunc("/getstatus", ReplyStatus)
http.ListenAndServe(":8088", mux)
}
NOTE : In a production system..... because the program is accessible from the web(outside world), you need to add some security measure such as password(with TFA) protected page or only accept incoming request from certain IP address.
References :
https://www.socketloop.com/tutorials/golang-gorilla-mux-routing-example
https://www.socketloop.com/tutorials/golang-web-routing-multiplex-example
See also : Golang : Web routing/multiplex example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.4k Javascript : Read/parse JSON data from HTTP response
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+19.2k Golang : How to count the number of repeated characters in a string?
+7k Golang : How to fix html/template : "somefile" is undefined error?
+13.6k Golang : convert(cast) string to float value
+7.3k SSL : How to check if current certificate is sha1 or sha2 from command line
+9.9k Golang : Text file editor (accept input from screen and save to file)
+17.1k Golang : Get future or past hours, minutes or seconds
+12.7k Golang : Get terminal width and height example
+14.8k JavaScript/JQuery : Detect or intercept enter key pressed example
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+4.4k Mac OSX : Get disk partitions' size, type and name