Background Service Setup
Service Setup
Create a Service File
my_service_name.service
[Unit]
# Description is a human-readable name for the service.
Description=Service Description
# Wait until dependencies are running and the network is "up" before starting the service.
After=postgresql.service
After=network-online.target
Wants=network-online.target
# Configure service start rate limiting. If the service is (re)started more than 5 times
# in 600 seconds then don't permit it to start anymore.
StartLimitIntervalSec=600
StartLimitBurst=5
[Service]
# Execute the API binary as the your_user_name user, loading the environment variables from
# /etc/environment and using the working directory /home/your_user_name.
Type=exec
User=your_user_name
Group=your_user_name
EnvironmentFile=/etc/environment
WorkingDirectory=/home/your_user_name
ExecStart=/home/your_user_name/your_binary
# Example
# ExecStart=/home/your_user_name/api -port=4000
# Automatically restart the service after a 5-second wait if it exits with a non-zero
# exit code. If it restarts more than 5 times in 600 seconds, then the rate limit we
# configured above will be hit and it won't be restarted anymore.
Restart=on-failure
RestartSec=5
[Install]
# Start the service automatically at boot time (the 'multi-user.target' describes a boot
# state when the system will accept logins).
WantedBy=multi-user.target
Move the Service to Correct Location on Server
sudo mv ~/my_service_name.service /etc/systemd/system/
Enable the Service
sudo systemctl enable my_service_name
Restart Service
sudo systemctl restart my_service_name
Checking the Background Service Logs
sudo journalctl -u my_service_name
example:
sudo journalctl -u api