Introduction
Services can be created in Linux using systemd services. Custom unit configuration files created by the system administrator can be made under the ‘/etc/systemd/system’ directory and then managed using the systemctl tool.
Practical Example
I want to create a systemd service for a Palworld server.
Create systemd service:
vim /etc/systemd/system/palworld.service
Contents of ‘palworld.service’:
[Unit]
Description=Palworld Server
After=network.target
[Service]
User=steam
Group=steam
WorkingDirectory=/home/steam/Steam/steamapps/common/PalServer
ExecStart=/home/steam/Steam/steamapps/common/PalServer/PalServer.sh
TimeoutStartSec=120
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
After=network.targetdelays start until networking is up;Restart=alwaysrelaunches the process if it exits, andRestartSecsets the delay between attempts.
Reload systemd:
systemctl daemon-reload
Enable service:
systemctl enable palworld.service
Start service:
systemctl start palworld.service
systemctl enable --now palworld.serviceenables and starts in a single step.
Managing the Service
Check status:
systemctl status palworld.service
Stop / restart:
systemctl stop palworld.service
systemctl restart palworld.service
Disable (stop it starting at boot):
systemctl disable palworld.service
Follow live logs:
journalctl -u palworld.service -f
After editing a unit file, always run
systemctl daemon-reloadbefore restarting the service.
References
- https://www.freedesktop.org/software/systemd/man/latest/systemd.service.html