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
- Find out which systemd you have
- Read a service's status
- Ask one yes-or-no question
- Check whether it starts at boot
- Check for failure directly
- See what's actually running
- See everything installed
- Write something to supervise
- Describe the service to systemd
- Put the files where systemd looks
- Look at it before starting
- Start the service
- Confirm it came up
- Restart it
- Stop the service
- Enable it at boot
- Confirm the boot wiring
- Disable and stop in one go
- Read the unit systemd sees
- Ask for one exact property
- Override without touching the file
- See the override merged in
- Mask a unit so it can't start
- Unmask and bring it back
- Reload config without a restart
- Read a service's logs
- Show only the last few lines
- Watch logs as they happen
- Narrow the logs to a time window
- Put the newest first
- Filter by severity
- Limit to the current boot
- Change the output format
- Read the kernel's own log
- See how much disk the journal uses
- Trim old journal data
- Manage your own user services
- Diagnose a service that won't stay up
- Find the cause in the journal
- Make it your own