The systemd service file that is triggered before the shutdown.target
(note that systemd targets are somewhat similar to RunLevels of SysVinit):
# /etc/systemd/system/unmount-webdrives-on-shutdown.service
[Unit]
Description=Unmount webdrives before shutdown
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/bin/bash /usr/local/bin/unmount-webmounts.sh
[Install]
WantedBy=multi-user.target
This is the script that is responsible for unmounting the webdrives, in this case some FUSE WebDAV mounts:
$ cat /usr/local/bin/unmount-webmounts.sh
#!/bin/bash
MNTS=(
'/mnt/webdav.example.com
'/mnt/webdav.somewhere.else
)
for mp in ${MNTS[@]}; do
mountpoint --quiet "$mp" && umount "$mp" || true
done
Last but not least, reload the systemd daemon systemctl daemon-reload
and enable the service sudo systemctl enable unmount-webdrives-on-shutdown.service
.