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:
You can run it using
docker
, map/expose the correct port(s) and you're done.Or, you can run it natively on the host system as a service; in this post, am sharing the snippets for that.
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!
- Previous: My helpful bash snippets
- Next: Configure and run xmrig in linux