Systemd for Beginners: Services, Timers, and Journal Logs
systemctl manages services and other units through systemd. This guide creates a small one-shot service that prints a message, then uses it to practice service status, startup, enablement, restart, and logs. The example does not change an existing service.
Check systemd and List Running Services
Start HereCheck whether process 1 is systemd, then see the installed systemd version and a sample of running services. The final command only lists units; it does not start or stop anything.
ps -p 1 -o comm=systemctl --versionsystemctl list-units --type=service --state=running --no-pagerβ― Illustrative Output (Results Vary)
systemdsystemd 255 (255.4-1ubuntu8) UNIT LOAD ACTIVE SUB DESCRIPTION cron.service loaded active running Regular background program processing daemon ssh.service loaded active running OpenBSD Secure Shell serverWrite a One-Shot Unit File
Practice UnitCreate a service named tutorial-hello.service. It runs /usr/bin/echo once and stays in the active (exited) state so you can inspect it after the command finishes. The unit is installed under /etc/systemd/system, which is the usual location for administrator-created system units.
# Stop if this unit name already exists; choose a new lab name instead.unit_state=$(systemctl show tutorial-hello.service -p LoadState --value)test "$unit_state" = not-found || { echo 'Unit name already exists; stop.' >&2; exit 1; }sudo test ! -e /etc/systemd/system/tutorial-hello.service || exit 1sudo test ! -L /etc/systemd/system/tutorial-hello.service || exit 1
sudo tee /etc/systemd/system/tutorial-hello.service >/dev/null <<'EOF'[Unit]Description=One-shot service for a systemd tutorial
[Service]Type=oneshotExecStart=/usr/bin/echo Hello from the tutorial serviceRemainAfterExit=yes
[Install]WantedBy=multi-user.targetEOF
sudo systemctl daemon-reloadsystemctl cat tutorial-hello.serviceβ― Illustrative Output (Results Vary)
[Unit]Description=One-shot service for a systemd tutorial
[Service]Type=oneshotExecStart=/usr/bin/echo Hello from the tutorial serviceRemainAfterExit=yes
[Install]WantedBy=multi-user.targetStart and Check the Example Unit
Runtime Statestart runs a unit now. status reports whether it is loaded, enabled, and active, plus recent log messages. Because this is a one-shot service with RemainAfterExit=yes, expect active (exited) after the echo command completes.
sudo systemctl start tutorial-hello.servicesystemctl status tutorial-hello.service --no-pagerβ― Illustrative Output (Results Vary)
β tutorial-hello.service - One-shot service for a systemd tutorial Loaded: loaded (/etc/systemd/system/tutorial-hello.service; disabled) Active: active (exited) Process: 1842 ExecStart=/usr/bin/echo Hello from the tutorial service (code=exited, status=0/SUCCESS)Enable on Boot and Restart Safely
Startupenable βnow both enables the unit for its configured boot target and starts it immediately. The example uses systemctl restart to run the one-shot command again.
sudo systemctl enable --now tutorial-hello.servicesystemctl is-enabled tutorial-hello.servicesudo systemctl restart tutorial-hello.servicesystemctl status tutorial-hello.service --no-pagerβ― Illustrative Output (Results Vary)
Created symlink /etc/systemd/system/multi-user.target.wants/tutorial-hello.service β /etc/systemd/system/tutorial-hello.service.enabledβ tutorial-hello.service - One-shot service for a systemd tutorial Loaded: loaded (/etc/systemd/system/tutorial-hello.service; enabled) Active: active (exited)Follow the Journal and Clean Up
Logs and CleanupRun the journal command in one terminal. It prints the most recent 50 entries for this unit, then follows new entries. In a second terminal, restart the service and watch its message arrive. Press Ctrl+C to stop following the journal.
sudo journalctl -u tutorial-hello.service -n 50 -f
# In a second terminal, generate a new entry:sudo systemctl restart tutorial-hello.service
# When finished, disable and stop the example, then remove its unit file:sudo systemctl disable --now tutorial-hello.servicesudo rm /etc/systemd/system/tutorial-hello.servicesudo systemctl daemon-reloadβ― Illustrative Output (Results Vary)
Oct 07 10:15:00 host systemd[1]: Starting One-shot service for a systemd tutorial...Oct 07 10:15:00 host echo[1842]: Hello from the tutorial serviceOct 07 10:15:00 host systemd[1]: Finished One-shot service for a systemd tutorial.