Godaemon, a Library to Daemonize Go Apps
August 27, 2013
Database
At SolarWinds® Database Performance Monitor we have a specific need to daemonize one of our Go applications, which must safely run detached from terminals, with a working directory that won’t hang a u-mount, and so forth.
This is variously said to be a bad idea in general, impossible to do in Go, or dangerous to do in Go if you hack it in with unsafe syscalls to fork. So we didn’t do it like that.
Instead, we made a library that cryptographically signs an environment variable, starts a copy of itself, and then exits. The copy of itself makes some environment modifications and starts another copy of itself, then exits. The final copy restores the original environment variable and then continues executing.
The result is a program that has all the properties of a daemon, but arrives at them in a different way than people usually try to do. It should be used carefully and only if you understand how these things work, but we’ve been running it in production with absolutely no problems for many months on many systems.
We hope this is helpful if you have a similar need!