How to Manage Services with systemd and systemctl

systemd is the service manager used by Ubuntu, Debian, and most modern Linux distributions to start, stop, restart, and monitor background services (Nginx, MySQL, SSH, Docker, and more). Understanding systemctl is a core Linux administration skill.

Checking a Service's Status

sudo systemctl status nginx

This shows whether it's active, when it started, and recent log output.

Starting, Stopping, and Restarting a Service

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

Reloading Configuration Without Downtime

For services that support it (like Nginx), reload applies config changes without dropping active connections:

sudo systemctl reload nginx

Enabling a Service to Start on Boot

sudo systemctl enable nginx

Enable and start in one command:

sudo systemctl enable --now nginx

Disabling a Service from Starting on Boot

sudo systemctl disable nginx

Checking If a Service Is Enabled

sudo systemctl is-enabled nginx

Listing All Running Services

systemctl list-units --type=service --state=running

Finding Failed Services

sudo systemctl --failed

This is especially useful right after a reboot to catch anything that didn't start correctly.

Viewing a Service's Logs

sudo journalctl -u nginx

Follow logs live:

sudo journalctl -u nginx -f

Show only the last hour:

sudo journalctl -u nginx --since "1 hour ago"

Creating a Custom systemd Service

Useful for running your own application as a managed background service:

sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target

[Service]
ExecStart=/usr/bin/node /opt/myapp/index.js
Restart=always
User=deploy
WorkingDirectory=/opt/myapp

[Install]
WantedBy=multi-user.target

Reload systemd and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now myapp

Common Errors

"Unit not found" — check the exact service name with systemctl list-units --type=service.

Custom service fails to start — check logs with journalctl -u myapp -n 50 and verify the ExecStart path is correct and executable.

Best Practices

  • Always check systemctl status after making a config change, before assuming it worked
  • Use reload instead of restart when available, to avoid dropping connections
  • Set Restart=always on custom services so they recover automatically from crashes

FAQ

What's the difference between restart and reload?
restart fully stops and starts the service (brief downtime); reload re-reads configuration without interrupting active connections, when the service supports it.

Related Articles

  • How to Read and Analyze Linux Logs with journalctl
  • How to Reboot a Linux VPS Safely
  • How to Schedule Tasks with Cron on a Linux VPS
  • systemd, systemctl, linux services, server administration
  • 0 משתמשים שמצאו מאמר זה מועיל
?האם התשובה שקיבלתם הייתה מועילה

מאמרים קשורים

How to Update and Upgrade Your Ubuntu or Debian VPS

Keeping your VPS updated is one of the most important and simplest maintenance tasks — it...

How to Enable and Configure Swap on a Linux VPS (Ubuntu & Debian)

Swap is disk space Linux can use as overflow memory when physical RAM is exhausted. It won't make...

How to Read and Analyze Linux Logs with journalctl

On modern Ubuntu and Debian systems, journalctl is the central tool for viewing system and...

How to Schedule Tasks with Cron on a Linux VPS

Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log...

How to Check and Manage Disk Usage on a Linux VPS

Running out of disk space can bring down databases, break log writing, and crash applications....