I am trying to setup a systemd unit that sends a shutdown command to my remote server using ssh. I have already searched the web for days now and found some snippets to create the following code in
[Unit] Description=runs only on shutdown Before=multi-user.target After=network.target Conflicts=shutdown.target [Service] Type=oneshot ExecStart=/bin/true ExecStop=/path/to/script.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target
After creating or editing it, I always reload the daemon
systemctl daemon-reload before enabling the unit to load it at boot. However, the script is loaded and no error occurs but the shutdown command is not send to the server once the pi shuts down.
What is wrong with this code; why is the bash script not executed? script.sh has the following content:
#! /bin/bash ssh -t user@remote_machine "sudo shutdown -P now" echo 'shut down command send to server exit 0'
When executed manually using the command line this script works as expected and the server shuts down.
I really hope that someone here knows how to accomplish this task, I am relatively sure that it might not be too far off the right approach.
Many thanks in advance!