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)

Commandsystemctl (Modern)service (Legacy)
Startsudo systemctl start nginxsudo service nginx start
Stopsudo systemctl stop nginxsudo service nginx stop
Restartsudo systemctl restart nginxsudo service nginx restart
Reload configsudo systemctl reload nginxsudo service nginx reload
Statussystemctl status nginxservice nginx status
Enable at bootsudo systemctl enable nginxsudo update-rc.d nginx defaults
Disable at bootsudo systemctl disable nginxsudo update-rc.d nginx remove

Tip: On modern Ubuntu, service is a compatibility wrapper that calls systemctl internally. Use systemctl directly 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

FieldSectionDescription
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