Logo
Loading...
Published on

Running Golang app as a service

Author

Running golang app as a service

Create service file

sudo nano /etc/systemd/system/blog.service

Set up your configuration

[Unit]
Description=Blog Service
After=syslog.target network.target mysql.service

[Service]
Type=simple
Restart=always
RestartSec=5s
PIDFile=/run/blog.ineersa.com.pid

StandardOutput=append:/var/log/blog.ineersa.com/output.log
StandardError=append:/var/log/blog.ineersa.com/error.log


ExecStart=/var/www/blog.ineersa.com/bin/blog
WorkingDirectory=/var/www/blog.ineersa.com

[Install]
WantedBy=multi-user.target

You can add env if you need to inside [Service] block

Environment=MY_GO_ENV=staging
...

Reload the systemd daemon to recognize the new service:

sudo systemctl daemon-reload

Start the service:

sudo systemctl start blog.service

Enable the service to start on boot:

sudo systemctl enable blog.service

To check the status of service and ensure it’s running correctly:

sudo systemctl status blog.service

To check service logs you can go to syslog or use:

journalctl -u blog.service