Services (.service)

Run a one-shot task at boot (Type=oneshot)

3 min read

Some tasks aren’t long-running daemons — they run once, do something (apply sysctl tweaks, warm a cache, run a migration), and exit. Type=oneshot is built for that.

The unit

Save as /etc/systemd/system/firstboot-setup.service:

[Unit]
Description=One-time boot setup
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/step-one.sh
ExecStart=/usr/local/bin/step-two.sh

[Install]
WantedBy=multi-user.target

Enable / run

sudo systemctl daemon-reload
sudo systemctl enable --now firstboot-setup.service
systemctl status firstboot-setup.service     # shows "active (exited)"

To re-run it later, just sudo systemctl restart firstboot-setup.service.


Use it with a timer: a oneshot service is exactly what a .timer activates on a schedule (see the timer recipes). Gotchas: don’t set Restart= on a oneshot — it’s meant to exit. If you need ordering before the network or a mount, use Before=/After= and the right target.

Open the full version (with copy buttons) ↗

← All recipes