How to talk between computers?

Here is my case:

I have a server (running LMDE) and a laptop (EndeavourOS). Now the server is a file server and also every minute checks for power outage.

If I am home, I know if my power is out, but if I am not, I don’t, so I want the server to let my laptop know the power is out (and also let my PC know, but I assume the same solution can be used)

Right now, my solution goes like this:

  • if the server detects an outage, it creates a file in a directory the laptop has access to
  • laptop checks every minute for that file
  • if it is there, it notifies me

But that feels bloated and hacky, so is there a better way?

Thanks in advance.

Let the server send an email if the power is out.

EMAIL="your@emailaddress"
EMAILMESSAGE="/tmp/body.txt"

echo "To: ${EMAIL}" >>${EMAILMESSAGE}
echo "From: root@your-fileserver" >>${EMAILMESSAGE}
echo "Subject: ${SUBJECT}" >>${EMAILMESSAGE}
echo "Importance: High" >>${EMAILMESSAGE}
echo "X-Priority: 1" >>${EMAILMESSAGE}
echo >>${EMAILMESSAGE}

echo "This is the text in the email" >>${EMAILMESSAGE}

sendmail -t <${EMAILMESSAGE}
rm ${EMAILMESSAGE}

PS
if you use a cronjob to check the powerstatus you can make use of the cron email feature. If cron executes a script it captures every “echo” and sends it via email to the root account of that server. Now you only need an alias in /etc/aliases or /etc/postfix/aliases, depending which OS and which MTA you use, and the email is automatically forwarded from root to that alias.

e.g. for the aliases on my debian server

# cat /etc/aliases

                  
# See man 5 aliases for format
postmaster:    root
root: my@personal.email.address

is there a way that doesn’t require my input to know that?

I sometimes leave my PC doing something while I am away, and if the power is out it should shut down before the UPS battery runs out

You mean the laptop needs to shutdown before the server is out of power?

No. I also have a PC (other than the server), and sometimes I run a job on it and leave it alone. And if the power is out, it should shut down before the UPS runs out of battery power.

And the PC is in the same network as the server?

Yes.

Than it is easy: Just let the server shutdown the PC.

You can do this with this command from the server:

ssh your-PC-ip "systemctl poweroff"

This works without password if you are using PubkeyAuthentication

This should be in your sshd_config:

PubkeyAuthentication yes
PermitRootLogin prohibit-password

You need to exchange the ssh keys of course.

I tried shutting it down like this, but got

Call to PowerOff failed: Access denied as the requested operation requires interactive authentication. However, interactive authentication has not been enabled by the calling program.

So give the userid permission to shutdown the computer. Read the man page and/or arch wiki for details, though ‘wheel’ might be the correct answer.

Which user did you use? Should be root

You can also try the plain poweroff command.

Depending on the make and model of your UPS, there are some Network UPS Tools available that might be an easier approach to achieve the desired functionality, including network monitoring. And automatic (safe) shutdown, depending on the state of charge of the UPS itself. Therefore, I’ld recommend to approach this from the angle of the actual make and model of the UPS.

Yeah, nut server is the solution here. Place it on your server, and let it do its stuff. Beat combines with a small ups solution. But you need to have some network working without power. For that I use an UPS which powers my router, my main switch and my raspi with nut.

This is nuts.

( I simply couldn’t refuse :laughing: )

Now you being nutty!

(couldn’t resist as well, I am a simple man)

Your comment made me realise I need a working network for any of this, and I can’t plug the router into the UPS (it’s in a different location)

So I just made my PC shut down in three minutes if it loses Internet connection and I am not using it locally.