Find us on social media
SystemdServicesLinux6 min read
Manage services with systemd
Manage system services with systemd: start, stop, enable, and create your own custom services.
systemd is the init system on modern Linux distributions. It manages services, handles dependencies and provides logging.
Basic service commands
bash
sudo systemctl start nginx # Start a service
sudo systemctl stop nginx # Stop a service
sudo systemctl restart nginx # Restart a service
sudo systemctl reload nginx # Reload config (no downtime)
sudo systemctl status nginx # Check status
sudo systemctl enable nginx # Start on boot
sudo systemctl disable nginx # Don't start on bootList services
bash
systemctl list-units --type=service
systemctl list-units --type=service --state=runningCreate a custom service
Create a service file for your application:
bash
sudo nano /etc/systemd/system/myapp.serviceContent:
ini
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapp
ExecStart=/usr/bin/node /home/deploy/myapp/dist/index.js
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
Environment=PORT=3000
[Install]
WantedBy=multi-user.targetEnable and start the custom service
bash
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myappView logs with journalctl
bash
# All logs for a service
sudo journalctl -u myapp
# Follow logs in real-time
sudo journalctl -u myapp -f
# Logs since last boot
sudo journalctl -u myapp -b
# Last 50 lines
sudo journalctl -u myapp -n 50
# Logs from the last hour
sudo journalctl -u myapp --since "1 hour ago"Service file options
ini
Restart=always # Always restart
RestartSec=10 # Wait 10s before restart
StartLimitBurst=5 # Max 5 restarts
StartLimitIntervalSec=60 # Within 60 seconds
StandardOutput=journal # Log to journalctl
StandardError=journalTroubleshooting
bash
# Check for errors in service file
sudo systemd-analyze verify myapp.service
# See why a service failed
sudo systemctl status myapp
sudo journalctl -u myapp --no-pager -n 30Tip
For Node.js apps, systemd is a lighter alternative to PM2. For Python apps, combine with Gunicorn. Always set Restart=on-failure for production services on your Baires Host VPS.
Was this guide helpful?