Here systemd will execute /srv/app_shutdown_script.sh
before the system reboots. Refer to the following steps.
- Create a systemd Unit file with the following content.
# cat /etc/systemd/system/exec-before-shutdown.service
[Unit]
Description=Shutdown run script
DefaultDependencies=no
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/srv/app_shutdown_script.sh
TimeoutStartSec=0
[Install]
WantedBy=shutdown.target
- Here we instructed the Unit file to run the script
/srv/app_shutdown_script.sh
before shutdown.target.
$ cat /srv/app_shutdown_script.sh
#!/bin/bash
c=0
for i in {1..3}; do
sleep 1m
((c++))
echo ""
done
- Reload the systemd configuration files.
# systemctl daemon-reload
- Enable the Unit to automatically start at next boot.
# systemctl enable exec-before-shutdown.service
Now reboot the system and confirm.