General Basics
Systemd controls what is running on modern Linux systems.
Service Management
| Command |
Action |
systemctl start myservice.service |
Start a service |
systemctl stop myservice.service |
Stop a service |
systemctl restart myservice.service |
Restart a service |
systemctl status myservice.service |
Show service status |
systemctl enable myservice.service |
Enable at boot |
systemctl disable myservice.service |
Disable at boot |
Listing Units
| Command |
Action |
systemctl list-units --type=service |
List active services |
systemctl --all list-unit-files --type=service |
List all services (including inactive) |
systemctl list-sockets |
List sockets |
Reloading Config
After modifying a unit file, reload the daemon:
sudo systemctl daemon-reload
Example: Tomcat Service Unit File
View the full Tomcat unit file
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/java
Environment=CATALINA_PID=/usr/local/TomcatRootDir/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/TomcatRootDir
Environment=CATALINA_BASE=/usr/local/TomcatRootDir
WorkingDirectory=/usr/local/TomcatRootDir
ExecStart=/usr/local/TomcatRootDir/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
Power / Reboot
| Command |
Action |
sudo systemctl poweroff |
Shut down the system |
sudo systemctl reboot |
Reboot the system |
Logs (journalctl)
| Command |
Action |
journalctl -xe |
Show recent logs with context |
journalctl --follow |
Tail logs live |
journalctl -u myservice.service |
Logs for a specific service |
journalctl -u myservice.service --since today |
Logs since midnight |
journalctl -u myservice.service -n 100 |
Last 100 lines |
journalctl --disk-usage |
Show how much space logs are using |
journalctl --vacuum-time=7d |
Delete logs older than 7 days |
Unit File Anatomy
A full service unit has three sections:
View the full unit file
[Unit]
Description=My Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/myapp
Restart=on-failure
RestartSec=5s
User=myuser
[Install]
WantedBy=multi-user.target
Type= value |
Meaning |
simple |
Process started by ExecStart is the main process |
forking |
Process forks; parent exits (e.g. traditional daemons) |
oneshot |
Short-lived task; systemd waits for it to exit |
notify |
Service signals readiness via sd_notify() |
Timers (cron replacement)
Create a .timer unit alongside a .service unit of the same name:
View the timer example
# myapp.timer
[Unit]
Description=Run myapp every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
| Command |
Action |
systemctl list-timers |
List all active timers |
systemctl enable --now myapp.timer |
Enable and start a timer |
Targets (runlevels)
| Target |
Equivalent |
multi-user.target |
Runlevel 3 — multi-user, no GUI |
graphical.target |
Runlevel 5 — multi-user with GUI |
rescue.target |
Runlevel 1 — single user |
systemctl get-default |
Show current default target |
systemctl set-default graphical.target |
Change default target |