Skip to content

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.


01

Check systemd and List Running Services

Start Here

Check 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.

Terminal window
ps -p 1 -o comm=
systemctl --version
systemctl list-units --type=service --state=running --no-pager
❯ Illustrative Output (Results Vary)
systemd
systemd 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 server

02

Write a One-Shot Unit File

Practice Unit

Create 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.

Terminal window
# 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 1
sudo 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=oneshot
ExecStart=/usr/bin/echo Hello from the tutorial service
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
systemctl cat tutorial-hello.service
❯ Illustrative Output (Results Vary)
/etc/systemd/system/tutorial-hello.service
[Unit]
Description=One-shot service for a systemd tutorial
[Service]
Type=oneshot
ExecStart=/usr/bin/echo Hello from the tutorial service
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

03

Start and Check the Example Unit

Runtime State

start 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.

Terminal window
sudo systemctl start tutorial-hello.service
systemctl 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)

04

Enable on Boot and Restart Safely

Startup

enable β€”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.

Terminal window
sudo systemctl enable --now tutorial-hello.service
systemctl is-enabled tutorial-hello.service
sudo systemctl restart tutorial-hello.service
systemctl 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)

05

Follow the Journal and Clean Up

Logs and Cleanup

Run 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.

Terminal window
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.service
sudo rm /etc/systemd/system/tutorial-hello.service
sudo 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 service
Oct 07 10:15:00 host systemd[1]: Finished One-shot service for a systemd tutorial.

Comments