Most modern Linux distributions (including Ubuntu, Debian, Rocky Linux, and CentOS) use systemd as their default init system and service manager. The primary utility used to interact with systemd is systemctl.
Whether managing a web server like Nginx, a database like MySQL, or a custom background daemon on your RivinCloud VPS, mastering systemctl is essential.
1. Starting, Stopping, and Restarting Services
To manipulate a running service, pass the action followed by the service name:
Start a Service
sudo systemctl start nginx
Stop a Running Service
sudo systemctl stop nginx
Restart a Service (Full Stop & Start)
sudo systemctl restart nginx
Reload Configuration Without Dropping Active Connections
sudo systemctl reload nginx
[!TIP] Always prefer
reloadoverrestartwhen updating web server configuration files in production, asreloadparses the new config without terminating existing client connections.
2. Managing Startup Behavior (Boot Persistence)
By default, starting a service only runs it for the current boot session. To ensure it persists across reboots:
Enable Service at Server Boot
sudo systemctl enable nginx
Disable Service from Booting Automatically
sudo systemctl disable nginx
Enable and Start in One Single Command
sudo systemctl enable --now nginx
3. Checking Service Status and Diagnostics
Check if a Service is Active
systemctl status nginx
This command outputs:
- Active state:
active (running),inactive (dead), orfailed - Main PID: The master process ID
- Memory & Tasks: Total memory consumed by the unit
- Recent Logs: The last 10 log entries outputted by the service
Quick Status Checks (Ideal for Bash Scripts)
systemctl is-active nginx # Returns 'active' or 'inactive'
systemctl is-enabled nginx # Returns 'enabled' or 'disabled'
systemctl is-failed nginx # Returns 'active' or 'failed'
4. Listing Services and Units
To see all services managed on your server:
# List all running services
systemctl list-units --type=service --state=running
# List all enabled services set to run on boot
systemctl list-unit-files --type=service --state=enabled
# List all failed services
systemctl --failed
5. Creating a Custom systemd Service
Creating your own systemd unit lets you run Node.js apps, Python bots, or Go binaries with automatic crash recovery.
Create a unit file:
sudo nano /etc/systemd/system/mybot.serviceAdd the unit configuration:
[Unit] Description=My Discord Bot Daemon After=network.target [Service] Type=simple User=ubuntu WorkingDirectory=/home/ubuntu/mybot ExecStart=/usr/bin/node /home/ubuntu/mybot/index.js Restart=always RestartSec=5 [Install] WantedBy=multi-user.targetReload systemd daemon and start:
sudo systemctl daemon-reload sudo systemctl enable --now mybot
Common Mistakes
- Forgetting
sudo systemctl daemon-reload: Whenever you edit or create a.servicefile, systemd will not recognize your changes until you reload the daemon configuration. - Using
killinstead ofsystemctl stop: IfRestart=alwaysis configured in the service file, systemd will simply treat a manualkillas a crash and restart the process immediately.
Troubleshooting
- Service enters
failed (Result: exit-code)state: Runjournalctl -u <service_name> -e --no-pagerto read the exact traceback or error output that caused the daemon to crash. - Service masked: If you see
Unit is masked, runsudo systemctl unmask <service_name>to unblock it.
Best Practices
- Always test your application manually in the terminal before wrapping it in a systemd service.
- Set sensible
RestartSecandRestart=on-failurepolicies to prevent infinite rapid crash-loops from pegging CPU cores.
Frequently Asked Questions
What is the difference between /etc/systemd/system/ and /lib/systemd/system/?
/lib/systemd/system/ contains unit files installed by software packages (APT/YUM). /etc/systemd/system/ is reserved for system administrators and overrides package defaults.
How do I view logs for a specific service?
Use journalctl -u <service-name> -f to follow logs live in real time.
Conclusion
systemctl is the backbone of Linux service management. With it, you can ensure your mission-critical applications run continuously and recover reliably on your RivinCloud VPS.
