tech katta

Services and logs: systemctl and journalctl

Lesson 09 of 23 · 5 min readFree

In this lesson

Start, stop, restart and enable services with systemctl, write a small unit file for your own app, and read its logs with journalctl.

On this page
  1. Why this matters
  2. The commands you will use
  3. Reading status
  4. A unit file for your app
  5. journalctl: reading the logs
  6. Seeing all services
  7. Try it

A service is a program that runs in the background, starts at boot and is restarted if it crashes: nginx, PostgreSQL, your own API. On most modern Linux servers a program called systemd manages them. You talk to it with systemctl and read what the services printed with journalctl.

Why this matters#

In lesson 8 you started a process with nohup. That works until the server reboots or the process crashes at 3 a.m. and nobody restarts it. Running your app as a systemd service fixes both problems, and gives you one standard place to read its logs. Almost every Ubuntu and Debian server you will deploy to works this way.

The commands you will use#

systemctl takes an action and a service name. The .service suffix is optional.

  • systemctl status myapp shows whether it is running and its latest log lines.
  • sudo systemctl start myapp starts it now, and sudo systemctl stop myapp stops it.
  • sudo systemctl restart myapp stops and starts. sudo systemctl reload nginx asks the program to reread its config without dropping connections, if it supports that.
  • sudo systemctl enable myapp makes it start at boot. sudo systemctl enable --now myapp enables it and starts it immediately.
  • sudo systemctl disable myapp removes it from boot.
  • systemctl is-active myapp and systemctl is-enabled myapp print one word, handy in scripts.

Starting and enabling are separate ideas: start changes what is running now, enable changes what happens at the next boot. Forgetting enable is the classic reason an app works after your deploy and vanishes after the next reboot.

Reading status needs no sudo. Changing things does.

Reading status#

A healthy service looks like this:

Text
● myapp.service - Tech Katta example API
     Loaded: loaded (/etc/systemd/system/myapp.service; enabled; preset: enabled)
     Active: active (running) since Wed 2026-12-02 10:15:03 UTC; 2h 3min ago
   Main PID: 2210 (node)
      Tasks: 11 (limit: 4557)
     Memory: 74.2M
        CPU: 3.512s
     CGroup: /system.slice/myapp.service
             └─2210 /usr/bin/node server.js

Dec 02 10:15:03 web01 systemd[1]: Started myapp.service - Tech Katta example API.
Dec 02 10:15:04 web01 node[2210]: API listening on port 5100

Read it top to bottom. Loaded says which unit file was used and whether it is enabled. Active is the key line: active (running) is good. Main PID is the process id from lesson 8, so you can use ps or kill on it. The last lines are the most recent log output.

A crashed service shows Active: failed (Result: exit-code) or activating (auto-restart) if it is in a restart loop. Both mean: go and read the logs.

A unit file for your app#

systemd learns about a service from a text file called a unit file. Files you write yourself go in /etc/systemd/system/. This one runs a Node API as the deploy user from the layout used in lesson 2:

ini
[Unit]
Description=Tech Katta example API
After=network.target

[Service]
User=deploy
WorkingDirectory=/srv/app/current
EnvironmentFile=/srv/app/shared/.env
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

What each part does:

  • After=network.target makes systemd start the service after networking is up.
  • User=deploy runs the app as an ordinary user, not root, which limits the damage if the app is compromised. Lesson 7's file permissions now matter: deploy must be able to read the code and the .env.
  • WorkingDirectory is the folder the process starts in. Services do not start in your home folder, which is one reason to use absolute paths.
  • EnvironmentFile loads KEY=value lines as environment variables, so secrets stay out of the unit file.
  • ExecStart is the command, written with a full path. systemd starts the program itself, not through your login shell, so it never sees the PATH your .bashrc builds. Recent versions can find a bare name in a fixed set of system folders, but a Node installed under your home folder with a version manager will not be found. Write /usr/bin/node and check yours with which node.
  • Restart=on-failure restarts the app if it exits with an error, waiting RestartSec seconds first.
  • WantedBy=multi-user.target is what enable hooks into: "start this in normal multi-user boot".

We ran this file through systemd-analyze verify, which checks syntax and that the files it refers to exist. It first reported Command /usr/bin/node is not executable, because the test container has no Node installed, and passed once we pointed it at a command that exists. You can run the same check on a real server before you use the file.

After creating or editing a unit file, tell systemd to reread its files, then enable and start:

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

Skipping daemon-reload is a common mistake: systemd keeps working from its old copy of the file and says nothing.

journalctl: reading the logs#

systemd collects everything a service writes to stdout and stderr into a journal. That is why a typical app does not need its own log file: print to the console, and journalctl has it.

Shell
journalctl -u myapp -n 20
journalctl -u myapp -f
journalctl -u myapp --since "1 hour ago"
journalctl -u myapp -p err
journalctl -b -u myapp
  • -u picks the unit.
  • -n 20 shows the last 20 lines.
  • -f follows new lines, like tail -f from lesson 4. Press Ctrl+C to stop.
  • --since and --until filter by time. They accept "2026-12-02 10:00", yesterday or "30 min ago".
  • -p err shows only entries of priority err and worse.
  • -b limits output to the current boot, and -b -1 is the previous boot, useful after a crash that caused a reboot.
  • --no-pager prints straight to the terminal, which suits pipes and scripts.

Here is what a startup failure usually looks like. Suppose an old copy of the app still holds port 5100:

Text
Dec 02 10:20:11 web01 systemd[1]: Started myapp.service - Tech Katta example API.
Dec 02 10:20:12 web01 node[2301]: Error: listen EADDRINUSE: address already in use :::5100
Dec 02 10:20:12 web01 systemd[1]: myapp.service: Main process exited, code=exited, status=1/FAILURE
Dec 02 10:20:12 web01 systemd[1]: myapp.service: Failed with result 'exit-code'.
Dec 02 10:20:17 web01 systemd[1]: myapp.service: Scheduled restart job, restart counter is at 1.

The error message EADDRINUSE says another process already listens on port 5100. The next lesson shows how to find which one with ss. The line with status=1/FAILURE is the exit code from lesson 1, and Scheduled restart job is Restart=on-failure doing its work. If your app keeps crashing quickly, systemd eventually gives up and reports start request repeated too quickly. Read the first error in the log, not the last one.

The journal also holds system logs. journalctl -k shows kernel messages and journalctl -u ssh shows login attempts. Old-style text logs still exist, such as /var/log/nginx/access.log, because some programs write their own files. For those, use the tools from lessons 4 to 6.

To see how much space the journal uses, run journalctl --disk-usage. You can cap it with sudo journalctl --vacuum-size=500M.

Seeing all services#

systemctl list-units --type=service --state=running lists what is running. systemctl --failed lists services that have failed, which is a good first command on a server that "feels wrong".

Try it#

  1. On a Linux machine with systemd, run systemctl status ssh (or sshd on some distributions) and identify the Loaded, Active and Main PID lines.
  2. Run systemctl --failed. If anything is listed, run journalctl -u <name> -n 30 --no-pager and find the first error.
  3. Write the unit file above for a simple script that prints a line and sleeps, for example ExecStart=/bin/sh -c 'while true; do echo alive; sleep 5; done'. Install it, daemon-reload, enable --now, then follow it with journalctl -u myapp -f.
  4. Kill the service's main process with sudo kill -9 <Main PID> and watch systemd restart it. Check the new PID with systemctl status.
  5. Run systemd-analyze verify /etc/systemd/system/myapp.service after a deliberate typo in ExecStart and read what it says.