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 withjournalctl. - 👉 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 type | Example | What it is for |
|---|---|---|
.service | nginx.service | A program that keeps running |
.timer | backup.timer | Run a service on a schedule (the modern cron) |
.socket | docker.socket | Start a service when a connection arrives |
.target | multi-user.target | A group of units, like a stage of booting |
.mount | data.mount | A 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● 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: 812How 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.
| Command | What it does |
|---|---|
systemctl start nginx | Start now |
systemctl stop nginx | Stop now |
systemctl restart nginx | Stop, then start (short downtime) |
systemctl reload nginx | Re-read the config without stopping (if the app supports it) |
systemctl enable nginx | Start at boot |
systemctl enable --now nginx | Start at boot and start now, in one step |
systemctl disable nginx | Do not start at boot |
systemctl is-active nginx | Prints active or inactive (handy in scripts) |
systemctl --failed | List every failed unit |
systemctl daemon-reload | Re-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| Command | Shows |
|---|---|
journalctl -u nginx | Logs for one service (-u = unit) |
journalctl -u nginx -f | Follow live, like tail -f |
journalctl -u nginx --since "10 min ago" | Only recent logs |
journalctl -u nginx -n 50 | The last 50 lines |
journalctl -p err | Only errors (and worse), from every service |
journalctl -b | Everything since the last boot |
journalctl -b -1 | The boot before that (why did it crash?) |
journalctl --disk-usage | How 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.targetThe 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.
| Line | Why it is there |
|---|---|
After=network-online.target | Wait until the network is up |
User=orders | Run as its own non-root user |
EnvironmentFile= | Keep settings and secrets out of the unit file |
Restart=on-failure | Restart automatically if it crashes |
RestartSec=5 | Wait 5 seconds between restarts |
LimitNOFILE | Allow more open files, for busy services |
WantedBy=multi-user.target | Where enable hooks it into the boot |
Then switch it on:
sudo systemctl daemon-reloadsudo systemctl enable --now orderssystemctl status ordersjournalctl -u orders -fTimers: 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.targetOnCalendar=*-*-* 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-timersshows 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 nginxsystemctl 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 dependencystatustells you that it is failing.journalctltells you why.- 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. startwithoutenable. It works now, and is gone after the next reboot.- Running services as root when they do not need it.
Restart=alwayson a broken app. It hides the problem in an endless restart loop. Watchjournalctland the restart count.
Practice
- Check the status of nginx and say whether it starts at boot.
- Restart nginx, then show its last log lines with
journalctl. - Write a unit file for a script
/opt/scripts/health.shthat restarts on failure. - List all failed units on a live lab server.
Cheat sheet
| Command | What it does |
|---|---|
systemctl status svc | Health, boot setting and recent logs |
systemctl restart / reload svc | Restart / apply config |
systemctl enable --now svc | Start now and at boot |
systemctl --failed | All failed services |
systemctl daemon-reload | Pick up edited unit files |
journalctl -u svc -f | Follow a service's logs |
journalctl -p err -b | All errors since boot |
Key takeaways
- systemd is the boss that starts your programs and restarts them if they crash.
systemctlis the remote control: start, stop, restart, status.startmeans now.enablemeans every time the server boots.journalctl -u nameshows a service's logs. Look there first when something fails.- To run your own app properly, write a small unit file for it.