Skip to main content
ramit's kb

Run a golang executable as a service in linux

Heyo. Imagine you have a shiny new golang project. You build the binary and now you want to run it. But how?

There are two ways:

Create the service (unit) file #

First, create the unit file your_app.service at /etc/systemd/system. Modify and add the below snippet to the file.


[Unit]
Description=Your Golang App
After=network.target

[Service]
User=your_user
WorkingDirectory=/path/to/your/app
ExecStart=/path/to/your/app

StandardOutput=append:/path/to/your/logfile/app.log
StandardError=append:/path/to/your/logfile/app.error.log

[Install]
WantedBy=multi-user.target

Enable the service #

Next job is to enable the service.


sudo systemctl daemon-reload
sudo systemctl start your_app.service

If it starts correctly then, yay, it's great! Now enable the service to auto start when the system boots.


sudo systemctl enable your_app.service

Finally, reboot your system and once it's booted, check if the service is up.


sudo systemctl status your_app.service

Check the app log at your configured location and that's it. FIN!