xen xcp-ng – How to run a script before shutdown

  linux, xcp-ng

Using Ubuntu 18.

Calling this service “shutdown”

sudo nano /etc/systemd/system/shutdown.service

Contents. Include the full path to your down.sh shell script. In this example, it’s /opt/settings/scripts/down.sh, and it calls a service using curl which then speaks to a speaker that the server is shutting down. 

[Unit]
Description=My shutdown

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/opt/settings/scripts/down.sh

[Install]
WantedBy=multi-user.target

Enable the service:

sudo systemctl daemon-reload
sudo systemctl enable shutdown --now

Test it:

sudo shutdown -r now