Golang : Terminate-stay-resident or daemonize your program?
Back in the old days of DOS, the concept of terminate-stay-resident via interrupt 27 is popular among programmers looking to write programs that does not terminate but remain active in the memory and can be easily invoked with couple of keys combinations. Programs such as multi-tasking utilities or viruses normally utilised terminate-stay-resident concept. In Unix/Linux world, the equivalent concept is call ...daemonizing your program.
Found this interesting package (https://github.com/icattlecoder/godaemon) from a Golang Facebook page on how to daemonize your Go program easily. All you have to do is to include the package and then add the parameter -d=true
when launching your program.
For example :
package main
import (
_ "github.com/icattlecoder/godaemon" //<---here!
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello, golang!\n"))
})
log.Fatalln(http.ListenAndServe(":8080", mux))
}
and
./example -d=true
~$ curl http://localhost:8080/index
hello, golang!
Now, you might ask ...won't it be easier to add '&'
symbol at the end of the program to make it into a background program in Linux/Unix?
A reply in the Facebook group provides a reasonable answer :
"As far as I know, for a daemon, the process is not tied to anything except init itself. With command & the process will be killed by a SIGHUP signal when the parent dies (in this case terminal). The very first process to be started on a Unix system is init, the daemon being a child of that process means that it is not under your direct control as an non-privileged user. Running it as a daemon causes the process to fork from the init itself."
Hope this simple tutorial can be useful to you!
References :
https://www.facebook.com/groups/206770519471402/
https://github.com/icattlecoder/godaemon
http://en.wikipedia.org/wiki/Terminateandstayresidentprogram
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
+5.5k Golang : How to determine if request or crawl is from Google robots
+4.8k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+7.3k Golang : On lambda, anonymous, inline functions and function literals
+6.9k Golang : Tell color name with OpenCV example
+12.4k Golang : Check if an integer is negative or positive
+25.3k Golang : dial tcp: too many colons in address
+22.7k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+7.1k Golang : HTTP Server Example
+13.3k Golang : package is not in GOROOT during compilation
+3.5k Adding Skype actions such as call and chat into web page examples
+8.6k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+5.6k PHP : Shuffle to display different content or advertisement