Skip to content

Instantly share code, notes, and snippets.

@hardiksondagar
Created June 6, 2020 10:15
Show Gist options
  • Save hardiksondagar/647b7b21b16c923e6d3138943e0f3950 to your computer and use it in GitHub Desktop.
Save hardiksondagar/647b7b21b16c923e6d3138943e0f3950 to your computer and use it in GitHub Desktop.
Create systemd service for Go binary

Create file at /lib/systemd/system/mygoservice.service

[Unit]
Description=mygoservice
ConditionPathExists=$GOPATH/src/mygoservice/mygoservice
After=network.target
 
[Service]
Type=simple
User=mygoservice
Group=mygoservice
LimitNOFILE=1024

Restart=on-failure
RestartSec=10
startLimitIntervalSec=60

WorkingDirectory=$GOPATH/src/mygoservice
ExecStart=$GOPATH/src/mygoservice/mygoservice --name=foo

# make sure log directory exists and owned by syslog
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/mygoservice
ExecStartPre=/bin/chown syslog:adm /var/log/mygoservice
ExecStartPre=/bin/chmod 755 /var/log/mygoservice
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=mygoservice
 
[Install]
WantedBy=multi-user.target

Enable and start service

sudo systemctl enable mygoservice.service
sudo systemctl start mygoservice
# check status
sudo journalctl -f -u mygoservice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment