A unit is a small text file
To make systemd manage your own program, write a unit file in /etc/systemd/system/. It has sections in square brackets: [Unit] describes it, [Service] says how to run it (ExecStart is the command), and [Install] says when it should start at boot:
sudo tee /etc/systemd/system/heartbeat.service > /dev/null <<'EOF'
[Unit]
Description=Write a heartbeat every few seconds
[Service]
ExecStart=/bin/sh -c 'while true; do date > /run/heartbeat; sleep 3; done'
[Install]
WantedBy=multi-user.target
EOF
cat /etc/systemd/system/heartbeat.serviceIn the terminal
student@vulntech:~$ sudo tee /etc/systemd/system/heartbeat.service > /dev/null <<'EOF' > [Unit] > Description=Write a heartbeat every few seconds > > [Service] > ExecStart=/bin/sh -c 'while true; do date > /run/heartbeat; sleep 3; done' > > [Install] > WantedBy=multi-user.target > EOF [sudo] password for student: student@vulntech:~$ cat /etc/systemd/system/heartbeat.service [Unit] Description=Write a heartbeat every few seconds [Service] ExecStart=/bin/sh -c 'while true; do date > /run/heartbeat; sleep 3; done' [Install] WantedBy=multi-user.target
Tell systemd, then start it
After writing or changing a unit, run systemctl daemon-reload so systemd re-reads its files. Then start it like any other service and check it's working:
sudo systemctl daemon-reload
sudo systemctl start heartbeat
systemctl is-active heartbeat
cat /run/heartbeatIn the terminal
student@vulntech:~$ sudo systemctl daemon-reload [sudo] password for student: student@vulntech:~$ sudo systemctl start heartbeat student@vulntech:~$ systemctl is-active heartbeat active student@vulntech:~$ cat /run/heartbeat Sun Oct 18 06:52:35 UTC 2026
Tip:
Type=oneshotwithRemainAfterExit=yessuits a job that runs once and finishes (a backup, a setup step) rather than a program that stays running. The default,Type=simple, is for a long-running program like the one above.
Remember: A unit file in
/etc/systemd/system/NAME.servicehas[Unit],[Service](withExecStart=) and[Install]sections. Runsystemctl daemon-reloadafter editing it, thenstart/enableit.