← All bricks

systemctl · Beginner

systemctl & journalctl: Services and Logs with systemd

Drive systemd from the command line — read a service's status, start and stop units, write and enable your own service, then chase what it does through the journal with journalctl.

On any modern Linux box, systemd is what starts your services and journald is where their logs go. Most people poke at them with half-remembered commands and hope. We fix that from the ground up: reading the status block line by line, controlling units with start and stop and restart, writing a real unit file and enabling it at boot, masking, drop-ins, and then living inside journalctl — filtering by unit, by time, by priority, by boot, following in real time, and trimming the journal when it grows. We finish by diagnosing a service that keeps falling over, using only these two tools. Writing a unit file and running it against systemd are taught as separate steps, so each idea lands on its own.

What you'll build

  • Read a unit's status block and check its state in scripts
  • Start, stop, restart and reload services
  • Write a unit file, reload systemd, and enable it at boot
  • Mask units and override them with drop-ins
  • Read a service's logs with journalctl filters
  • Follow, format and trim the journal
  • Diagnose a flaky service from status and logs alone

Contents

  1. Find out which systemd you have
  2. Read a service's status
  3. Ask one yes-or-no question
  4. Check whether it starts at boot
  5. Check for failure directly
  6. See what's actually running
  7. See everything installed
  8. Write something to supervise
  9. Describe the service to systemd
  10. Put the files where systemd looks
  11. Look at it before starting
  12. Start the service
  13. Confirm it came up
  14. Restart it
  15. Stop the service
  16. Enable it at boot
  17. Confirm the boot wiring
  18. Disable and stop in one go
  19. Read the unit systemd sees
  20. Ask for one exact property
  21. Override without touching the file
  22. See the override merged in
  23. Mask a unit so it can't start
  24. Unmask and bring it back
  25. Reload config without a restart
  26. Read a service's logs
  27. Show only the last few lines
  28. Watch logs as they happen
  29. Narrow the logs to a time window
  30. Put the newest first
  31. Filter by severity
  32. Limit to the current boot
  33. Change the output format
  34. Read the kernel's own log
  35. See how much disk the journal uses
  36. Trim old journal data
  37. Manage your own user services
  38. Diagnose a service that won't stay up
  39. Find the cause in the journal
  40. Make it your own