Linux β€’ Web Apps & APIs

How to Create a Systemd Service for Go (Golang) Compiled Binary

Go compiles into standalone, self-contained ELF binaries with no external runtime dependencies. Systemd is the ideal companion to run Go binaries as lightweight background microservices with Linux security isolation directives.

/etc/systemd/system/go-service.service
[Unit]
Description=Go Production Microservice
After=network.target

[Service]
Type=exec
User=appuser
Group=appuser
WorkingDirectory=/opt/go-service
ExecStart=/opt/go-service/bin/server
Restart=on-failure
RestartSec=3
StandardOutput=journal
StandardError=journal
SyslogIdentifier=go-service
# Linux Security Sandboxing
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
Customize:
User:
WorkingDir:
ExecStart:

Systemctl Lifecycle & Journalctl Monitoring

sudo systemctl daemon-reloadNotify systemd of changes
sudo systemctl enable --now go-serviceStart Go binary and enable persistence
sudo systemctl status go-serviceConfirm service status and memory usage

Linux & Systemd Production Best Practices

  • Use `Type=exec` for Go binaries. Systemd will consider the service started only when the binary has successfully executed the binary header.
  • Ensure the binary has executable permissions: `chmod +x /opt/go-service/bin/server`.

Frequently Asked Questions About Go Binary Systemd Service

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

They enforce Linux kernel security: ProtectSystem makes `/usr`, `/boot`, and `/etc` read-only for the process, while NoNewPrivileges prevents privilege escalation exploits.