Introduction
systemd is the init system of modern Linux distributions running as PID 1. It unifies service management, dependency resolution, logging, and timers.
Service unit files replaced SysV init scripts. systemctl starts, stops, enables, and queries services.
This guide covers unit file anatomy, [Install] section, override mechanism, and production best practices.
Unit File Structure
[Unit] defines Description, After, Before, Requires, Wants. [Service] has ExecStart, Restart, User, Type. [Install] WantedBy sets boot target.
Unit files live in /etc/systemd/system/ (admin) and /usr/lib/systemd/system/ (package). systemctl edit creates drop-in overrides.
[Unit]
Description=My Application
After=network.target postgresql.service
[Service]
Type=simple
User=deploy
ExecStart=/opt/app/bin/start.sh
Restart=on-failure
[Install]
WantedBy=multi-user.targetService Management Commands
systemctl start/stop/restart/reload manages the service. enable/disable controls auto-start at boot. status shows detailed state and recent logs.
daemon-reload is mandatory after unit file changes. list-units --type=service lists all services.
- start: başlat
- enable: boot'ta aç
- reload: yapılandırma yenile
- is-active: durum kontrol
sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service
sudo systemctl status myapp.service
journalctl -u myapp.service -fDependencies and Ordering
After=postgresql.service starts this service after postgresql; Requires= stops this service if postgresql fails. Wants= is a weak dependency.
multi-user.target is the normal server boot target. graphical.target includes desktop environment.
[Unit]
Requires=network-online.target
After=network-online.target docker.service
Wants=redis.serviceLog Monitoring with journalctl
systemd journal is the central log store. journalctl -u servis.service filters service logs. -f follows live; --since '1 hour ago' filters by time.
PersistentStorage=yes in /etc/systemd/journald.conf keeps logs after reboot.
journalctl -u nginx.service --since today
journalctl -p err -b
journalctl -u myapp -o json-prettyTimer and Socket Units
.timer unit is a cron alternative; triggered by OnCalendar or OnUnitActiveSec. .socket unit provides socket activation; starts service on request.
systemctl list-timers --all shows active timers.
ExecStart öncesi ortam değişkenleri Environment= veya EnvironmentFile= ile tanımlayın; shell export güvenilir değildir.
Production Best Practices
Automatic recovery with Restart=on-failure or always. LimitNOFILE for file descriptor limit. Security hardening: NoNewPrivileges, PrivateTmp.
Run services as dedicated user not root. ExecStartPre for migration or prep steps.
- Dedicated kullanıcı
- Restart politikası
- journalctl izleme
- daemon-reload unutmayın
- Override ile minimal değişiklik
Conclusion
Learning systemd service management is fundamental to modern Linux operations. Keep unit files in version control and deploy with infrastructure-as-code.
Monitor logs centrally with journalctl; modernize cron jobs with timers.