systemd.unit
Reference manpage: man systemd.unit
systemd.unit - Unit configuration
Create service user and group
It is highly advised to create separate service user and limit it's access only that is necessary to operate Service accounts are unable to log in
useradd --system --shell /usr/sbin/nologin --no-create-home <username>
Creating a new systemd service
This involves defining a "unit file" with a .service extension.
These files are typically placed in /etc/systemd/system/ for system-wide services, or ~/.config/systemd/user/ for user-specific services.
Here's an example of a simple service definition (e.g., /etc/systemd/system/myapp.service):
[Unit] Description=My custom service After=network.target [Service] ExecStart=/opt/app/app.py --port 8080 Restart=on-failure RestartSec=5s WorkingDirectory=/opt/app #Type=simple #StandardOutput=journal #User=user #Group=group #Environment="MY_ENV_VAR=production" #LimitNOFILE=65536 #MemoryLimit=256M [Install] WantedBy=default.target
Enable and start service
After creating the service file, you need to tell systemd to reload its configuration and then enable/start your service
sudo systemctl daemon-reload sudo systemctl enable myapp sudo systemctl start myapp sudo systemctl status myapp