TL;DR — Quick Summary
Essential Ubuntu service management commands: systemctl start/stop/enable/disable, journalctl log viewing, listing services, and creating custom systemd unit files.
Ubuntu Service Management Commands
Managing services (daemons) is one of the most common tasks for any Ubuntu administrator. Modern Ubuntu uses systemd as the init system, with systemctl as the primary command for service management.
systemctl vs. service (Legacy)
| Command | systemctl (Modern) | service (Legacy) |
|---|---|---|
| Start | sudo systemctl start nginx | sudo service nginx start |
| Stop | sudo systemctl stop nginx | sudo service nginx stop |
| Restart | sudo systemctl restart nginx | sudo service nginx restart |
| Reload config | sudo systemctl reload nginx | sudo service nginx reload |
| Status | systemctl status nginx | service nginx status |
| Enable at boot | sudo systemctl enable nginx | sudo update-rc.d nginx defaults |
| Disable at boot | sudo systemctl disable nginx | sudo update-rc.d nginx remove |
Tip: On modern Ubuntu,
serviceis a compatibility wrapper that callssystemctlinternally. Usesystemctldirectly for the richest output and full feature set.
Essential systemctl Commands
Check Service Status
systemctl status nginx
This shows: active/inactive state, PID, memory usage, CPU time, and the last few log lines.
Start, Stop, Restart, Reload
sudo systemctl start nginx # Start a stopped service
sudo systemctl stop nginx # Stop a running service
sudo systemctl restart nginx # Stop then start (brief downtime)
sudo systemctl reload nginx # Reload config without downtime (not all services support this)
Enable / Disable at Boot
sudo systemctl enable nginx # Start automatically on boot
sudo systemctl disable nginx # Do NOT start on boot
sudo systemctl enable --now nginx # Enable AND start immediately
sudo systemctl is-enabled nginx # Check if enabled
List All Services
# List all loaded services and their states
systemctl list-units --type=service
# List only running services
systemctl list-units --type=service --state=running
# List all installed service files (including disabled)
systemctl list-unit-files --type=service
Masking a Service
To completely prevent a service from being started (even manually):
sudo systemctl mask dangerous-service # Prevent start entirely
sudo systemctl unmask dangerous-service # Allow start again
Viewing Logs with journalctl
systemd captures all service output via journald. Use journalctl to view logs:
# View all logs for a service
journalctl -u nginx
# Follow logs in real time (like tail -f)
journalctl -u nginx -f
# Today's logs only
journalctl -u nginx --since today
# Last hour
journalctl -u nginx --since "1 hour ago"
# Last 50 lines
journalctl -u nginx -n 50
# Show only errors and above
journalctl -u nginx -p err
# Disk usage of all logs
journalctl --disk-usage
Creating a Custom systemd Service
If you have a custom application (e.g., a Node.js app, a Go binary, or a Python script) that you want to run as a service:
Step 1: Create the Unit File
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Custom Application
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/start.sh
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Step 2: Reload and Enable
sudo systemctl daemon-reload # Read the new unit file
sudo systemctl enable --now myapp # Enable at boot + start now
systemctl status myapp # Verify it is running
Key Unit File Fields
| Field | Section | Description |
|---|---|---|
After | [Unit] | Start after these targets/services |
Type | [Service] | simple (default), forking, oneshot, notify |
User | [Service] | Run as this user (not root) |
ExecStart | [Service] | The command to run |
Restart | [Service] | on-failure, always, no |
RestartSec | [Service] | Seconds to wait before restart |
Environment | [Service] | Set environment variables |
WantedBy | [Install] | Target that enables this service |