Systemd
Pengantar Systemd
Systemd adalah sistem init dan service manager modern yang digunakan di hampir semua distribusi Linux utama (Debian, Ubuntu, Fedora, Arch). Systemd menggantikan SysV init dan Upstart, membawa paralelisasi booting, dependency-based startup, dan manajemen service yang lebih canggih.
PID 1 di Linux modern adalah systemd:
ps -p 1 -o comm= # Output: systemd
Unit Files
Systemd menggunakan unit files - file konfigurasi yang mendefinisikan resource sistem. Jenis unit utama:
| Unit Type | Ekstensi | Fungsi |
|---|---|---|
| Service | .service |
Daemon / aplikasi latar belakang |
| Socket | .socket |
IPC atau network socket (aktivasi berbasis socket) |
| Timer | .timer |
Jadwal eksekusi (pengganti cron) |
| Mount | .mount |
Mount point filesystem |
| Path | .path |
Aktivasi berdasarkan perubahan file |
| Target | .target |
Grup unit (pengganti runlevel) |
Lokasi unit files:
/lib/systemd/system/- Unit bawaan paket/etc/systemd/system/- Unit kustom/admin (lebih prioritas)
Contoh Service Unit
File /etc/systemd/system/myapp.service:
[Unit]
Description=My Custom Application
After=network.target
Wants=redis.service
[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/app --config /etc/myapp/config.yml
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
systemctl - Manajemen Service
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart
systemctl reload nginx # Reload konfigurasi (tanpa mati)
systemctl enable nginx # Auto-start saat boot
systemctl disable nginx # Nonaktifkan auto-start
systemctl status nginx # Status + log terkini
systemctl is-active nginx # Cek apakah aktif (active/inactive)
systemctl is-enabled nginx # Cek enable/disable
systemctl daemon-reload # Reload unit files setelah edit manual
List semua service:
systemctl list-units --type=service # Service aktif
systemctl list-units --type=service --all # Semua service
systemctl list-unit-files --type=service # Status enable/disable
journalctl - Log Management Systemd
Systemd mengelola log sendiri melalui journald:
journalctl -u nginx # Log service nginx
journalctl -u nginx -f # Follow (real-time)
journalctl -u nginx --since "1 hour ago" # Log 1 jam terakhir
journalctl -u nginx -p err # Filter level: emerg, alert, crit, err, warning, info, debug
journalctl --list-boots # Riwayat boot
journalctl -b -1 # Log dari boot sebelumnya
journalctl -k # Kernel log (dmesg)
journalctl --disk-usage # Cek ukuran log
Target (Runlevel)
Systemd target menggantikan runlevel SysV:
| Runlevel | Target Systemd |
|---|---|
| 0 | poweroff.target |
| 1 / S | rescue.target |
| 2, 3, 4 | multi-user.target |
| 5 | graphical.target |
| 6 | reboot.target |
systemctl get-default # Lihat default target
sudo systemctl set-default multi-user.target # Set default (no GUI)
sudo systemctl isolate graphical.target # Pindah target sekarang
Drop-in Configuration
Tanpa mengedit unit file asli (yang bisa tertimpa update), systemd mendukung drop-in konfigurasi:
mkdir -p /etc/systemd/system/nginx.service.d/
cat > /etc/systemd/system/nginx.service.d/override.conf << 'EOF'
[Service]
LimitNOFILE=65536
Environment=MY_CUSTOM_VAR=value
EOF
systemctl daemon-reload
systemctl restart nginx
Keuntungan drop-in: konfigurasi kustom tetap ada meskipun paket nginx di-upgrade.
Dependency dan Ordering
Systemd menyelesaikan dependency antar unit dengan direktif:
| Direktif | Fungsi |
|---|---|
Requires= |
Unit target harus berhasil start |
Wants= |
Opsional - start jika bisa, tetap lanjut jika gagal |
After= |
Unit ini start setelah unit target |
Before= |
Unit ini start sebelum unit target |
BindsTo= |
Jika unit target mati, unit ini ikut mati |
[Unit]
Description=Web App
Requires=postgresql.service redis.service
After=network.target postgresql.service redis.service
systemd-analyze
Tool analisis boot time dan dependency:
systemd-analyze # Total waktu boot
systemd-analyze blame # Waktu start per unit (descending)
systemd-analyze critical-chain # Chain kritis boot
systemd-analyze dot nginx.service | dot -Tsvg > graph.svg # Dependency graph
systemd-analyze verify /etc/systemd/system/myapp.service # Validasi unit file
systemd-analyze blame sangat berguna untuk
mengidentifikasi service yang memperlambat boot - optimasi bisa
berupa parallel startup atau delayed start dengan
After=.
Timer Unit - Pengganti Cron
# /etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
# /etc/systemd/system/backup.service
[Unit]
Description=Daily Backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
Aktifkan:
systemctl enable --now backup.timer
systemctl list-timers
Timer lebih unggul dibanding cron: logging terintegrasi journald, bisa mengejar jadwal yang terlewat (Persistent=true), dan dependency management.