| Tested on | RHEL 10.2 (Coughlan) — vm1.lab.example |
|---|---|
| Package | systemd 257-23.el10_2.2 |
| Applies to | Ubuntu, Debian, Kali Linux, Linux Mint, Pop!_OS, Raspberry Pi OS, elementary OS, Zorin OS, Parrot OS, MX Linux, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux |
| Privilege | sudo or root |
| Scope | Run a oneshot script only during poweroff or halt by enabling on poweroff.target and halt.target, and confirm reboot does not append a log line. Does not cover long-running services stopped with ExecStop. |
| Related guides | Run script before shutdown Linux boot reboot shutdown Create a systemd service systemctl command Beginners guide to systemd |
Poweroff and halt end the machine; reboot brings it back up. Both involve shutdown work, but you may want a script only when the system is going off — for example flushing a local backup before power is removed.
Enable the unit on poweroff.target and halt.target, not on reboot.target. That way reboot never pulls your service into its transaction.
Create the shutdown script
The script appends a timestamp when poweroff or halt runs it:
sudo tee /usr/local/sbin/shutdown-only-task.sh <<'EOF'
#!/bin/bash
LOG=/var/log/shutdown-only-task.log
echo "time=$(date -Is) event=poweroff_or_halt" >> "$LOG"
EOFMake it executable:
sudo chmod 755 /usr/local/sbin/shutdown-only-task.shCreate the systemd unit
Create /etc/systemd/system/shutdown-only-task.service:
[Unit]
Description=Run task at poweroff or halt only (not reboot)
DefaultDependencies=no
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/shutdown-only-task.sh
[Install]
WantedBy=poweroff.target halt.targetWantedBy=poweroff.target halt.target is what makes the service shutdown-only. systemctl enable creates links under those targets, but not under reboot.target, so reboot does not pull this unit in.
Before=shutdown.target places the script before the general shutdown synchronization point rather than at the very end of poweroff. If your task depends on another service or mount, give that dependency explicit ordering.
Reload and enable
Reload unit definitions after editing the file:
sudo systemctl daemon-reloadEnable the unit on poweroff and halt targets:
sudo systemctl enable shutdown-only-task.serviceSample output:
Created symlink '/etc/systemd/system/poweroff.target.wants/shutdown-only-task.service' → '/etc/systemd/system/shutdown-only-task.service'.
Created symlink '/etc/systemd/system/halt.target.wants/shutdown-only-task.service' → '/etc/systemd/system/shutdown-only-task.service'.Verify shutdown-only behavior
After enabling, the unit should run for poweroff or halt, but not for reboot.
Power off the test VM (console or hypervisor — not reboot):
sudo poweroffAfter the machine is back on, read the log:
sudo cat /var/log/shutdown-only-task.logSample output from lab testing:
time=2026-08-21T11:13:42+05:30 event=poweroff_or_haltIf you also test with reboot, no new timestamp should be added to that file — that confirms reboot did not start the unit.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Script runs on reboot | Unit enabled on shutdown.target instead |
Use WantedBy=poweroff.target halt.target only |
| No log line after poweroff | Unit not enabled | Confirm symlinks under poweroff.target.wants and halt.target.wants |
| Script also needed on reboot | Wrong guide | Use before shutdown unit instead |
References
Summary
Shutdown-only behavior comes from WantedBy=poweroff.target halt.target — that decides when the unit participates: poweroff and halt, but not reboot. Before=shutdown.target decides when inside those shutdown transactions it runs.
Verify with poweroff and check the log file. If you need the same script on reboot and poweroff, use the before shutdown guide instead.

