ArunNetworkingPro
🧟

A service that refuses to die

Like a zombie, but helpful. And it writes logs.

Real servers don't run programs in a terminal window you have to keep open. systemd starts them at boot, logs everything they say, and resurrects them when they crash. Let's build a tiny zombie.

Intermediate1 eveningsystemd
heartbeat.shsystemdthe guardiankill -9 💥I'll be back.🧟

🧰 What you need

Let's build it

1

Write a tiny program

sudo tee /usr/local/bin/heartbeat.sh > /dev/null <<'EOF'
#!/bin/bash
while true; do
  echo "still alive at $(date +%T)"
  sleep 10
done
EOF
sudo chmod +x /usr/local/bin/heartbeat.sh
2

Tell systemd about it

sudo tee /etc/systemd/system/heartbeat.service > /dev/null <<'EOF'
[Unit]
Description=My first service

[Service]
ExecStart=/usr/local/bin/heartbeat.sh
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF
3

Start it, and make it start at boot

sudo systemctl daemon-reload
sudo systemctl enable --now heartbeat
systemctl status heartbeat
4

Read what it says

journalctl -u heartbeat -f

A new line every 10 seconds. Press Ctrl+C to stop watching; the service keeps running.

✅ How you know it worked

systemctl status heartbeat shows active (running) in green, and systemctl is-enabled heartbeat says enabled, so it survives a reboot.

💥 Break it on purpose

Try to kill it:

sudo systemctl kill -s SIGKILL heartbeat
sleep 5
systemctl status heartbeat

It's back, with a brand-new process ID. Restart=always brought it back from the dead. To really stop it: sudo systemctl disable --now heartbeat.

🧠 What's really going on

systemd is the first program Linux starts, and it's in charge of every service. The unit file is a recipe: what to run, when to start it (WantedBy=multi-user.target means "at normal boot") and what to do if it dies. Everything the program prints goes to the journal automatically, which is why journalctl -u is the first place to look when a service misbehaves.

← Back to all Linux labs · Stuck? Email me