Skip to content
Laviyx Learn

Try

Linux and Bash course menu

systemd and systemctl Tutorial: Manage Linux Services and Logs

Start, stop and enable services with systemctl, read logs with journalctl, and write your own systemd service file so that an app runs at boot and restarts on failure.

Intermediate6 min readUpdated
On this page

A service is a program that runs in the background, such as a web server or database. On most Linux systems, services are managed by systemd, using the systemctl command.

Check a service#

Terminal
systemctl is-active cron
systemctl status cron --no-pager | head -n 3 | cut -c1-74
Output
active
● cron.service - Regular background program processing daemon
     Loaded: loaded (/usr/lib/systemd/system/cron.service; enabled; preset
     Active: active (running) since Thu 2026-11-05 06:22:12 UTC; 4min 22s

active (running) means that it is working, and enabled means that it starts at boot.

Control a service#

Terminal
sudo systemctl start nginx      # start now
sudo systemctl stop nginx       # stop now
sudo systemctl restart nginx    # stop and start
sudo systemctl reload nginx     # reload settings, no downtime
sudo systemctl enable nginx     # start at every boot
sudo systemctl disable nginx    # do not start at boot

start and enable are different. A service can run now and not start at the next boot, and the other way round. sudo systemctl enable --now nginx does both.

List services#

Terminal
systemctl list-units --type=service --state=running --no-pager --no-legend | wc -l
systemctl list-units --type=service --state=failed --no-pager --no-legend | wc -l
Output
26
0

Create your own service#

We will make a small program into a real service. First, the program, a script that writes a line every second:

Terminal
sudo tee /usr/local/bin/heartbeat.sh > /dev/null <<'EOF'
#!/bin/bash
while true; do
  echo "Heartbeat at $(date +%T)"
  sleep 1
done
EOF
sudo chmod +x /usr/local/bin/heartbeat.sh

Next, the unit file, which tells systemd how to run it:

Terminal
sudo tee /etc/systemd/system/heartbeat.service > /dev/null <<'EOF'
[Unit]
Description=Heartbeat demo
After=network.target

[Service]
ExecStart=/usr/local/bin/heartbeat.sh
Restart=on-failure
User=asha

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now heartbeat 2> /dev/null
sleep 2
systemctl is-active heartbeat
systemctl is-enabled heartbeat
Output
active
enabled
SettingMeaning
ExecStartThe exact command to run; use a full path
Restart=on-failureRestart it if it crashes
UserRun as this user, not root
WantedBy=multi-user.targetStart at normal boot

daemon-reload is needed each time you change a unit file.

This is how you run a Node.js or Python app on a server: ExecStart=/usr/bin/node /srv/app/server.js.

Read the logs#

Everything a service prints is collected in the journal, and read with journalctl:

Terminal
sudo journalctl -u heartbeat -n 2 --no-pager -o cat
Output
Heartbeat at 06:26:36
Heartbeat at 06:26:37
CommandShows
journalctl -u nginxOne service's logs
journalctl -u nginx -fFollow them live
journalctl -u nginx --since "1 hour ago"Recent logs
journalctl -p errErrors only
journalctl -bSince the last boot

Traditional log files#

Many programs also write to /var/log:

FileContains
/var/log/syslogGeneral system messages on Ubuntu
/var/log/auth.logLogins and sudo use
/var/log/nginx/Web server logs

Stop and clean up#

Terminal
sudo systemctl disable --now heartbeat 2> /dev/null
systemctl is-active heartbeat
sudo rm /etc/systemd/system/heartbeat.service /usr/local/bin/heartbeat.sh
sudo systemctl daemon-reload
Output
inactive

When a service fails#

  1. Run systemctl status name, and read the last lines.
  2. Run journalctl -u name -n 50.
  3. Run the ExecStart command by hand, as the same user.
  4. Check paths, permissions, and whether the port is already in use.

Common mistakes#

Practice#

Practice exercise

Print the status word for cron and ssh, then print the description of cron using systemctl show.

Show solution
Terminal
systemctl is-active cron ssh
systemctl show cron -p Description
Output
active
active
Description=Regular background program processing daemon

Key takeaway#

Next: Installing Software with apt.

Finished this lesson?