Skip to content
$ lynuxpro
← Linux

LESSON 9 OF 26 · INTERMEDIATE · 18 MIN

Services and logs with systemd

In one minute

  • systemd is the manager that starts and watches over programs (services) on almost every modern Linux server. It also collects their logs in one place, called the journal.
  • It starts nginx when the server boots, restarts your app if it crashes, and keeps every service's logs. If you run anything on a Linux server for real, you run it under systemd.
  • You describe each service in a small text file called a unit file. Then you control it with systemctl (start, stop, restart) and read its logs with journalctl.
  • 👉 Starting or restarting a service, making an app start at boot, and checking why a service failed.

Think of it like: a building manager. It opens the shops in the morning (boot), reopens any shop that suddenly closes (crash restart), and keeps a logbook of everything that happened (journal).

Units and services

systemd manages units. A unit is anything systemd looks after. The ones you will meet:

Unit typeExampleWhat it is for
.servicenginx.serviceA program that keeps running
.timerbackup.timerRun a service on a schedule (the modern cron)
.socketdocker.socketStart a service when a connection arrives
.targetmulti-user.targetA group of units, like a stage of booting
.mountdata.mountA mounted disk

You will use .service almost all the time.

Controlling services: systemctl

systemctl is the remote control for services.

👉 Every time you start, stop, restart or check a service.

systemctl status nginxsudo systemctl restart nginx
SHELLbashutf-8 · 2L
● nginx.service - nginx     Loaded: loaded (/lib/systemd/system/nginx.service; enabled)     Active: active (running) since Mon 2026-11-25 09:01:12 UTC; 3 days ago   Main PID: 812
OUTPUTterminalutf-8 · 4L

How to read status:

  • Loaded … enabled = it starts at boot. disabled = it does not.
  • Active: active (running) = healthy. failed = it crashed. inactive (dead) = stopped.
  • The last few log lines are shown at the bottom. They are often enough to spot the error.
CommandWhat it does
systemctl start nginxStart now
systemctl stop nginxStop now
systemctl restart nginxStop, then start (short downtime)
systemctl reload nginxRe-read the config without stopping (if the app supports it)
systemctl enable nginxStart at boot
systemctl enable --now nginxStart at boot and start now, in one step
systemctl disable nginxDo not start at boot
systemctl is-active nginxPrints active or inactive (handy in scripts)
systemctl --failedList every failed unit
systemctl daemon-reloadRe-read unit files after you edit them

Remember it: start = now. enable = at boot. You usually want both: enable --now.

Reload or restart? For web servers, prefer reload. It applies config changes without dropping visitors' connections.

Reading logs: journalctl

journalctl reads the journal, where systemd keeps the logs of every service.

👉 The first place to look when a service fails.

journalctl -u nginx
SHELLbashutf-8 · 1L
CommandShows
journalctl -u nginxLogs for one service (-u = unit)
journalctl -u nginx -fFollow live, like tail -f
journalctl -u nginx --since "10 min ago"Only recent logs
journalctl -u nginx -n 50The last 50 lines
journalctl -p errOnly errors (and worse), from every service
journalctl -bEverything since the last boot
journalctl -b -1The boot before that (why did it crash?)
journalctl --disk-usageHow much space the logs use

Writing your own service

A unit file that tells systemd how to run your app.

Running your app with nohup java -jar app.jar & is fragile. It will not come back after a crash or a reboot. A unit file fixes both.

Create /etc/systemd/system/orders.service:

[Unit]Description=Orders APIAfter=network-online.targetWants=network-online.target[Service]User=ordersWorkingDirectory=/opt/ordersEnvironmentFile=/etc/orders/orders.envExecStart=/usr/bin/java -jar /opt/orders/app.jarRestart=on-failureRestartSec=5LimitNOFILE=65536[Install]WantedBy=multi-user.target
NORMALconfutf-8 · 16L

The file has three parts: [Unit] = what it is and what it needs first, [Service] = how to run it, [Install] = when to start it at boot.

LineWhy it is there
After=network-online.targetWait until the network is up
User=ordersRun as its own non-root user
EnvironmentFile=Keep settings and secrets out of the unit file
Restart=on-failureRestart automatically if it crashes
RestartSec=5Wait 5 seconds between restarts
LimitNOFILEAllow more open files, for busy services
WantedBy=multi-user.targetWhere enable hooks it into the boot

Then switch it on:

sudo systemctl daemon-reloadsudo systemctl enable --now orderssystemctl status ordersjournalctl -u orders -f
OUTPUTterminalutf-8 · 4L

Timers: the modern cron

A timer runs a service on a schedule.

Why over cron: You get status and logs for free, and it can catch up on jobs it missed.

👉 Nightly backups, cleanups and reports.

# /etc/systemd/system/backup.timer[Timer]OnCalendar=*-*-* 02:00:00Persistent=true[Install]WantedBy=timers.target
NORMALconfutf-8 · 7L
  • OnCalendar=*-*-* 02:00:00 = every day at 2 a.m.
  • Persistent=true = if the machine was off at 2 a.m., run the job when it comes back.
  • systemctl list-timers shows when each timer runs next.

Real-world scenario: the app keeps restarting

An alert says the orders API keeps going up and down. Your steps:

systemctl status nginxjournalctl -u nginx
SHELLbashutf-8 · 2L
systemctl status orders                       # failed? how many restarts?journalctl -u orders --since "30 min ago"     # the actual error# e.g. "Could not connect to database"systemctl status postgresql                   # check the dependency
OUTPUTterminalutf-8 · 4L
  1. status tells you that it is failing.
  2. journalctl tells you why.
  3. Follow the clue (here, the database) until you find the real cause.

Common mistakes

  • Editing a unit file and forgetting daemon-reload. systemd keeps using the old version.
  • start without enable. It works now, and is gone after the next reboot.
  • Running services as root when they do not need it.
  • Restart=always on a broken app. It hides the problem in an endless restart loop. Watch journalctl and the restart count.

Practice

  1. Check the status of nginx and say whether it starts at boot.
  2. Restart nginx, then show its last log lines with journalctl.
  3. Write a unit file for a script /opt/scripts/health.sh that restarts on failure.
  4. List all failed units on a live lab server.

Cheat sheet

CommandWhat it does
systemctl status svcHealth, boot setting and recent logs
systemctl restart / reload svcRestart / apply config
systemctl enable --now svcStart now and at boot
systemctl --failedAll failed services
systemctl daemon-reloadPick up edited unit files
journalctl -u svc -fFollow a service's logs
journalctl -p err -bAll errors since boot

Key takeaways

  • systemd is the boss that starts your programs and restarts them if they crash.
  • systemctl is the remote control: start, stop, restart, status.
  • start means now. enable means every time the server boots.
  • journalctl -u name shows a service's logs. Look there first when something fails.
  • To run your own app properly, write a small unit file for it.