Processes & Services (systemd)

💤0
Lv 10 XP
← 🧱 Foundations · Linux Fundamentals

Processes & Services (systemd)

Intermediate ⭐ 80 XP ⏱ 20 min #linux#processes#systemd

Inspect running processes and manage long-running services with systemd.

📖Theory

A process is a running program with a unique PID. You inspect them with ps, top, or htop, and control them with signals sent via kill. The two you use most: SIGTERM (15, polite “please stop”) and SIGKILL (9, forceful and uncatchable).

Long-running background services are managed by systemd on modern Linux. Services are defined as units (.service files) and controlled with systemctl. Their logs go to the journal, read with journalctl.

  • systemctl start/stop/restart <svc> — control now
  • systemctl enable/disable <svc> — control at boot
  • systemctl status <svc> — see state + recent logs
🌍Real-World Example
ps aux --sort=-%cpu | head      # top CPU consumers
pgrep -a nginx                  # find a process by name
kill 4821                       # SIGTERM (graceful)
kill -9 4821                    # SIGKILL (force)

sudo systemctl status nginx     # is it running? recent logs
sudo systemctl restart nginx    # restart it
sudo systemctl enable --now nginx  # start now AND at boot
journalctl -u nginx -f          # follow this service's logs
✍️Hands-On Exercise
  1. Use ps aux to find the process using the most memory.
  2. Start a long process (e.g. sleep 600 &), find its PID, and terminate it gracefully.
  3. Check the status of a service like ssh or cron with systemctl status.
  4. Tail a service’s logs in real time with journalctl -u <name> -f.
🧾Cheat Sheet
TaskCommand
List processesps aux / top / htop
Find by namepgrep -a name
Graceful stopkill <PID>
Force killkill -9 <PID>
Service statussystemctl status svc
Start/stopsystemctl start|stop svc
Enable at bootsystemctl enable svc
Service logsjournalctl -u svc -f
💬Common Interview Questions
What's the difference between SIGTERM and SIGKILL?

SIGTERM (15) asks a process to stop and can be caught so it cleans up. SIGKILL (9) forcibly terminates it immediately and cannot be caught or ignored.

What's the difference between systemctl enable and start?

start runs the service now; enable configures it to start automatically at boot. enable --now does both.

How do you view logs for a single systemd service?

journalctl -u <service> (add -f to follow, --since today to filter by time).

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type