You are not logged in.

#1 2015-05-07 09:36:23

pie86
Member
Registered: 2009-09-06
Posts: 78

Systemd service to shutdown remote machines with ssh

Hi All,

I'm trying to shutdown two raspberryPis that are connected to my pc when I switch the pc off.

I've made a simple script that looks like

#!/bin/bash
ping -c 1 10.0.1.2
if [ $? -eq 0 ]
then
  ssh root@10.0.1.2 "/sbin/shutdown -hP now"
fi

ping -c 1 10.0.2.2
if [ $? -eq 0 ]
then
  ssh root@10.0.2.2 "/sbin/shutdown -hP now"
fi

and I would like to call it with systemd (of course there is password-less ssh and the script works fine when called).

However all the approaches I'm trying fails because the network is switched off before or while the script is executing.

Two possible systemd services follows:

[Unit]
Description=Shutdown Raspberries
Wants=network.target
Requires=network.target netctl-ifplugd@enp4s1.service netctl-ifplugd@enp4s0.service
Before=shutdown.target reboot.target halt.target
DefaultDependencies=no

[Service]
Type=oneshot
User=ansvi
ExecStart=/path/to/shutdown_rpis.sh
RemainAfterExit=yes
TimeoutSec=90

[Install]
WantedBy=shutdown.target reboot.target halt.target
[Unit]
Description=Shutdown Raspberries
Requires=network.target

[Service]
User=ansvi
ExecStart=/bin/true
ExecStop=/path/to/shutdown_rpis.sh
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

Did someone have similar issues and maybe found a solution? Can you correct/suggest a better way to do this?

Thanks
Pietro

Last edited by pie86 (2015-05-07 13:31:02)

Offline

Board footer

Powered by FluxBB