Services and logs: systemctl and journalctl
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
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 myappshows whether it is running and its latest log lines.sudo systemctl start myappstarts it now, andsudo systemctl stop myappstops it.sudo systemctl restart myappstops and starts.sudo systemctl reload nginxasks the program to reread its config without dropping connections, if it supports that.sudo systemctl enable myappmakes it start at boot.sudo systemctl enable --now myappenables it and starts it immediately.sudo systemctl disable myappremoves it from boot.systemctl is-active myappandsystemctl is-enabled myappprint 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:
● 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 5100Read 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:
[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.targetWhat each part does:
After=network.targetmakes systemd start the service after networking is up.User=deployruns the app as an ordinary user, not root, which limits the damage if the app is compromised. Lesson 7's file permissions now matter:deploymust be able to read the code and the.env.WorkingDirectoryis the folder the process starts in. Services do not start in your home folder, which is one reason to use absolute paths.EnvironmentFileloadsKEY=valuelines as environment variables, so secrets stay out of the unit file.ExecStartis the command, written with a full path. systemd starts the program itself, not through your login shell, so it never sees thePATHyour.bashrcbuilds. 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/nodeand check yours withwhich node.Restart=on-failurerestarts the app if it exits with an error, waitingRestartSecseconds first.WantedBy=multi-user.targetis whatenablehooks 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:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
systemctl status myappSkipping 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.
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-upicks the unit.-n 20shows the last 20 lines.-ffollows new lines, liketail -ffrom lesson 4. Press Ctrl+C to stop.--sinceand--untilfilter by time. They accept"2026-12-02 10:00",yesterdayor"30 min ago".-p errshows only entries of priorityerrand worse.-blimits output to the current boot, and-b -1is the previous boot, useful after a crash that caused a reboot.--no-pagerprints 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:
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#
- On a Linux machine with systemd, run
systemctl status ssh(orsshdon some distributions) and identify theLoaded,ActiveandMain PIDlines. - Run
systemctl --failed. If anything is listed, runjournalctl -u <name> -n 30 --no-pagerand find the first error. - 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 withjournalctl -u myapp -f. - Kill the service's main process with
sudo kill -9 <Main PID>and watch systemd restart it. Check the new PID withsystemctl status. - Run
systemd-analyze verify /etc/systemd/system/myapp.serviceafter a deliberate typo inExecStartand read what it says.
Sign in to keep track of the lessons you finish.