OSMC: Using systemd to shutdown remote server

Hello all,

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 /etc/systemd/system/:

Description=runs only on shutdown



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!

Check paths, and remember that the script will be run as OSMC. If you have an SSH key, make sure .ssh exists under /root

Easiest way to check if the script fires is to put touch /home/osmc/.ranscript in first line and check for existence on reboot.

Hi and thanks for the quick response.

As all of this is quite new to me and I have just started diving into the topics - can you provide some additional information on the first point?
As far as I know the keys for ssh are stored in /home/osmc/.ssh/.

Also, I would like to know, if the script.sh has to be stored in a defined location or if I can choose this randomly as long as the path is correct?

Thanks again!

The script runs as root, so either specify a path to the key, or move it under /root.

or add:


Under service

Specifying User=osmc under the [Service] section did finally the trick.

many thanks for your time and the information :wink: