Processes, services & systemd
Inspect processes, read logs with journalctl and write a systemd unit.
By Invicly Tech, , in Full DevOps with Python. About 10 minutes to read.
In short
A process is a running program with a PID, an owner and resources. systemd starts services at boot, restarts them when they crash and collects their logs, which journalctl lets you read.
Think of it like this: systemd is a building manager who starts every machine in the morning and restarts any that stall.
Explained simply
A program is a file on disk. A process is that program while it is running, with its own memory, its own ID number (the PID) and a user it runs as. Run python3 app.py twice and you get two processes from one program, each unaware of the other.
Processes form a family tree. Every process has a parent that started it, and the very first one, PID 1, is usually systemd. When your shell runs ls, the shell is the parent and ls is the child. If a parent dies, PID 1 adopts its orphans.
Servers need some programs to run all the time: web servers, databases, your API. These long-running background processes are called services or daemons. systemd is the caretaker that starts them at boot, restarts them if they crash, and keeps their output in a central log.
For DevOps work this matters every day. When a site is slow, you look for the process eating CPU. When a deploy fails, you read the service log. When you ship a new app to a plain VM, you write a small unit file so it survives reboots.
Press Play on a diagram to walk through it one step at a time.
- 1Write unit file
Create /etc/systemd/system/app.service with ExecStart.
- 2daemon-reload
Tell systemd to read new or changed unit files.
- 3enable --now
Start it now and on every boot.
- 4Check status
systemctl status shows state, PID and recent log lines.
- 5Read logs
journalctl -u app -f follows the service output live.
- Default for kill
- Asks politely to stop
- App can clean up
- Try this first
- kill -9
- Kernel stops it instantly
- No cleanup possible
- Last resort only
- Hang-up signal
- Many daemons reload config
- Behaviour depends on the app
- 1fork
Parent makes a copy of itself
- 2exec
The copy loads the new program
- 3Run and sleep
Works, waits for I/O, works again
- 4exit
Returns an exit code, 0 for success
- 5Reaped
Parent reads the code; PID is freed
- Back to step 1
- You to systemd: systemctl stop app1systemctl stop app
- systemd to app process: SIGTERM: please stop2SIGTERM: please stop
- app process to systemd: Finishes requests, exits 03Finishes requests, exits 0
- systemd to You: Unit is inactive4Unit is inactive
- systemd to app process: SIGKILL if still running after 90 s5SIGKILL if still running after 90 s
In depth
Process states you will see in ps and top
The STAT column in ps tells you what a process is doing. R means running or ready to run. S means sleeping, waiting for something like a network reply; most processes sit here. D means stuck waiting on disk or network storage and cannot be interrupted, which often points to a slow disk or a hung NFS mount. Z is a zombie: it has finished, but its parent has not yet collected its exit code.
Zombies use no CPU or memory, only a slot in the process table. A pile of them means the parent program has a bug; killing the parent clears them.
- R: running
- S: sleeping (normal)
- D: waiting on I/O, can't be killed
- Z: zombie, parent hasn't collected it
- T: stopped, for example by Ctrl+Z
Load average, explained
uptime and top show three numbers like 0.80 1.20 2.50. They are the average number of processes running or waiting over the last 1, 5 and 15 minutes. On Linux the count also includes processes in state D, so heavy disk waits raise load even when the CPU is idle.
Compare load with the number of CPU cores, which nproc prints. A load of 4 on a 4-core machine means it is fully busy. A load of 12 on that machine means work is queuing. Reading the three numbers left to right tells you if things are getting worse or calming down.
What makes a good systemd unit
A unit file has three parts. [Unit] describes the service and its order: After=network-online.target waits for the network. [Service] says how to run it: User=, WorkingDirectory=, ExecStart= with an absolute path, and Environment= or EnvironmentFile= for settings. [Install] says when to start it at boot, usually WantedBy=multi-user.target.
Restart=on-failure with RestartSec=5 brings a crashed app back after five seconds. systemd also stops restart loops: by default, more than 5 starts within 10 seconds marks the unit failed. When stopping, systemd sends SIGTERM and waits 90 seconds before SIGKILL; change that with TimeoutStopSec=.
- Use absolute paths in ExecStart
- Keep secrets in EnvironmentFile with mode 600
- systemctl cat app shows the unit systemd actually loaded
- systemctl edit app creates a safe override file
- [Unit]
- Description=
- After=network-online.target
- [Service]
- User=, WorkingDirectory=
- ExecStart= (absolute path)
- EnvironmentFile=
- Restart=on-failure, RestartSec=5
- [Install]
- WantedBy=multi-user.target
Getting more out of journalctl
journald stores logs with metadata, so you can filter them precisely. journalctl -u app --since "10 min ago" shows recent lines for one service. -p err shows only errors and worse. -b shows logs since the last boot, and -b -1 the boot before that, which is gold after a crash. -o json-pretty reveals every field, including the PID and the exact unit.
Logs can fill a disk too. journalctl --disk-usage shows the size, and SystemMaxUse=500M in /etc/systemd/journald.conf caps it. To keep logs across reboots, make sure /var/log/journal exists.
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Inspect processes with ps, top and htop~45m
- 2Send signals and kill stuck processes~45m
- 3Read service logs with journalctl~45m
- 4Write and enable a systemd unit~45m
Key points
- Use ps, top or htop to find a process, its PID and its CPU and memory use.
- Send SIGTERM first so the process can shut down cleanly; SIGKILL cannot be caught.
- A systemd unit with Restart=on-failure brings a crashed service back automatically.
- journalctl -u name shows a service's logs; add -f to follow or --since to filter.
Key terms
Tap a card to flip it. 0/5 known
Examples
[Unit]
Description=FastAPI app
After=network.target
[Service]
User=deploy
WorkingDirectory=/srv/app
ExecStart=/srv/app/.venv/bin/uvicorn main:app --port 8000
Restart=on-failure
[Install]
WantedBy=multi-user.targetSave as /etc/systemd/system/app.service, then systemctl enable --now app.
Something is eating the CPU
The server feels slow. Step 1: check load against cores with uptime and nproc. Step 2: list the top CPU users. Step 3: look at the parent and the full command so you know what it really is. Step 4: ask it to stop with SIGTERM and wait a few seconds. Step 5: only if it is still alive, use SIGKILL. If it belongs to a systemd service, use systemctl restart instead, so systemd stays in charge.
uptime; nproc
ps -eo pid,ppid,user,%cpu,%mem,stat,cmd --sort=-%cpu | head -5
ps -o pid,cmd --ppid 1234 # children of 1234
kill 4321 # SIGTERM
sleep 5; kill -0 4321 && kill -9 4321
systemctl status 4321 # which unit owns the PID?Run a clean-up every night with a systemd timer
Timers are systemd's replacement for cron. Step 1: write a oneshot service that runs your script once. Step 2: write a timer with the same name that says when. Persistent=true runs a missed job after the machine was off. Step 3: run systemctl daemon-reload, then systemctl enable --now cleanup.timer. Step 4: systemctl list-timers shows the next run, and journalctl -u cleanup shows each run's output.
# /etc/systemd/system/cleanup.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
# /etc/systemd/system/cleanup.timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.targetQuick reference
- pstree -p shows the parent and child tree with PIDs
- ps -eo pid,stat,cmd | awk '$2 ~ /Z/' lists zombie processes
- Load average above the number of cores (nproc) means work is queuing
- systemctl list-units --failed shows every broken service
- systemctl cat app shows the unit file systemd actually loaded
- journalctl -u app -p err --since today shows only today's errors
- journalctl -b -1 shows logs from the previous boot, useful after a crash
- systemctl list-timers shows scheduled jobs and their next run
Common mistakes
- Editing a unit file and forgetting systemctl daemon-reload.
- Reaching for kill -9 first, which can leave temp files and locks behind.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Start sleep 300 in the background, find its PID and its parent PID, then stop it with SIGTERM and confirm it is gone.
- Exercise 2medium
Write a systemd unit for python3 -m http.server 8080 that runs as user nobody in /tmp, restarts on failure after 3 seconds, and starts at boot. Check that it runs.
- Exercise 3hard
Prove that Restart=on-failure works and that systemd's restart limit kicks in. Kill the service from the previous task with SIGKILL several times quickly and explain what you see.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between a process and a thread?
- A process has its own memory space. Threads live inside one process and share its memory, so they are cheaper to create but can step on each other.
- Should I use cron or systemd timers?
- Both work. Timers give you logs in journalctl, catch-up runs with Persistent=true and dependency control, so they are the better default on systemd machines.
- Why does my service work when I run it by hand but fail under systemd?
- systemd runs it with a different user, working directory and a minimal environment, so your PATH and shell variables are missing. Use absolute paths and set Environment= or EnvironmentFile= in the unit.