Daemon
Application developed by go could be deployed with daemon mode. This is a common way when the application is a simple API server. The example here is using systemctl under Ubuntu.
Set server environment
To use your application, first you need to set your server’s environment, like initialing database or adding permissions.
Create project folder
Create a folder to store your executable file. We are creating it under /home/ubuntu/ here.
- api-server
- config.yaml
ℹ️
Remember to add your config file or static resources in the folder, or there will be some errors.
Create service
Add a service file to your /etc/systemd/system/ folder.
/etc/systemd/system/my-api.service
[Unit]
Description=SERVER API
# Change it to your database if needed
After=mariadb.service
[Service]
Type=simple
WorkingDirectory=/home/ubuntu/server/
ExecStart=/home/ubuntu/server/api-server
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.targetStart service
Start your service with:
sudo systemctl start my-api.serviceGoFrame provide graceful restart and shutdown functions under *nix system. You could use the following command to restart or shutdown your service gracefully:
- Restart service:
kill -SIGUSR1 ProcessID- Shutdown service:
kill -SIGTERM ProcessIDℹ️
You could also use other tools like
supervisor to manage your services.