here is an example mentioned by Jeremias approach
wait_for_shutdown.service file will be used to control the service
[Unit]
Description=Wait for the signal to shutdown system through a shared file
[Service]
Restart=on-failure
Type=simple
ExecStart=/opt/app-system/wait_for_shutdown.sh
RestartSec=5
[Install]
WantedBy=multi-user.target
wait_for_shutdown.sh file waits for a signal in the shared file
#!/bin/sh
echo "start wait_for_shutdown service"
echo "waiting" > /var/run/shutdown_signal
echo "start wait_for_shutdown service while-do"
while sleep 1; do
signal=$(cat /var/run/shutdown_signal)
if [ "$signal" == "true" ]; then
echo "wait_for_shutdown service - received shutdown"
echo "done" > /var/run/shutdown_signal
shutdown -h now
fi
done
echo "end wait_for_shutdown service while-do"
wait_for_shutdown_service_install.sh file - use this script to install the service
#!/bin/sh
mkdir -p /opt/app_system
cp ./wait_for_shutdown.sh /opt/app_system
sudo cp ./wait_for_shutdown.service /etc/systemd/system
sudo systemctl enable wait_for_shutdown.service
sudo systemctl start wait_for_shutdown.service