Skip to content
Advertisement

How do I run a simple Go server as a daemon in Debian?

I recently wrote a simple server in Go:

JavaScript

It works perfectly well if compiled and then executed by ./go_http_server &.

The problem is that it doesn’t survive reboots. So after some reading, I attempted to daemonize it by placing a script in /etc/init.d:

JavaScript

…then running update-rc.d go_http_server defaults, and poof! It runs on boot, as verified by ps -ef | grep go_http_server.

But it doesn’t receive GET requests while running as a service. Thinking it might be running before the network interface was up, I tried service go_http_server stop, followed by service go_http_server start; still refused to receive GET requests. Stopping the service again and then executing ./go_http_server & makes the server function correctly once more.

I’ve been Googling this on and off for a couple days now. Either my search queries suck, or this isn’t an obvious problem. How do I daemonize my Go server?


EDIT: The exact same thing happens with a server I wrote in Python: it works as it should when executed using ./python_server.py, but–if started as service–HTTP requests are ignored. Both files have been made executable, and it doesn’t matter if the daemon user is root or any other user. Not sure if this helps, but I thought it might be relevant.

Advertisement

Answer

Supervisor is a good fit here, and can automatically capture and rotate logs written to stdout, restart on crash and manage ports/permissions.

Here’s what an example configuration would look like for a Go web service:

JavaScript

I wrote an article[1] that takes you through the steps, but the Supervisor documentation is extremely comprehensive.

Similarly, Debian systems also use systemd[2], which can achieve this as well.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement