1

I have an Ubuntu 20.04 server, I'm connecting to a UPS. I want the Ubuntu server to shut down a truenas server, also on the same ups. The idea is that the UPS software on Ubuntu server triggers the shutdown on the trunas server when the battery is at 50%, The ubuntu server in turn triggers a shutdown of the trunas server, giving time for both boxes to power down gracefully.

I've set up the following script file truenas-off.sh:

echo "shutdown occurred at $(date)" >> /root/shutdown-events.log
ssh root@1.2.3.4 "midclt call system.shutdown"

I set the script up as described here, the only difference is I set mine to just shutdown.

cat  /etc/systemd/system/truenas-off.service
[Unit]
Description=Run truenas-off on shutdown

[Service] Type=oneshot RemainAfterExit=true ExecStart=/root/truenas-off.sh

[Install] WantedBy=multi-user.target

I've run ssh-keygen on the ubuntu server and ssh-copy-id root@truenas-IP and when I've issued sudo -i, authenticated, I can ssh root@trunasIP truenas-off.sh and the trunas turns off

It just doesn't seem to want to work automatically when I shutdown the Ubuntu server.

What am I doing wrong?

There's a similar problem, but not quite the same here: How to send shutdown command to servers

muru
  • 197,895
  • 55
  • 485
  • 740
Jujuh
  • 11
  • 1
    The page you followed is incomplete. You are missing a few things in the [unit] section of the script that tells systemd to run the service at shutdown. By default the service will run when the system boots. – user68186 Jan 30 '24 at 19:36
  • 2
  • In the script truenas-off.sh you have the command ssh root@1.2.3.4 "midclt call system.shutdown". Does 1.2.3.4 stand for trunasIP that you use later? Why do you run this script from within the Trunas manually by ssh root@trunasIP truenas-off.sh. Does the script truenas-off.sh stored in the Ubuntu server or in the Truenas? – user68186 Jan 30 '24 at 21:08
  • Please see the simpler second link the the duplicate question's answer: https://unix.stackexchange.com/a/41756/524752 Basically, just change "ExecStart" to "ExecStop". You can also look in journalctl to see if your service is running – Daniel T Jan 30 '24 at 23:18
  • @DanielT ExecStop= allows running a command (or multiple commands) when the service is stopped, not necessarily when the computer is stopped. It is important to understand the nuances. – user68186 Feb 01 '24 at 19:12

0 Answers0