3

Situation

The computer (running Ubuntu 14.04) is connected to a networked device which controls the power supply to the computer itself.

On receiving a certain signal A, the computer must send a signal B to this networked device which (among other stuff) will cut power to the computer.

Question

When should I send this signal B to the networked device so as to minimize the damage done from the power cut?

I assume I can trigger the shutdown sequence with signal A and somewhere along the shutdown sequence, send signal B. But when should I actually send it?

I assume my /etc/rc0.d would look something like below

K98signalB # Actually sends signalB to cut power
K99networking # Renamed from K01networking, since I need to keep networking alive
2
  • 1
    You could have a look at how nut (opensource UPS monitoring framework) does it. Commented Apr 1, 2019 at 13:14
  • Cutting power should be delayed, then you can just shutdown normally. Otherwise it may be a use case for shutdown ramfs (like initramfs in reverse). Commented Apr 1, 2019 at 13:45

1 Answer 1

1

Assuming linux

I looked into something similar to this some years ago. The question I cannot answer is whether networking will still be available at the right time.

To actually pull the plug on the power, instead of trying to hook into SystemD or sysvinit, you really should try to hook into the kernel directly with a kernel mod.

When I searched on this topic I eventually came up with some information here: How does the system shutdown of a linux kernel work internally?

This led me to kernel code here: https://elixir.bootlin.com/linux/v5.0.5/source/arch/arm64/kernel/process.c#L136

void machine_power_off(void)
{
    local_irq_disable();
    smp_send_stop();
    if (pm_power_off)
        pm_power_off();
}

To do this properly you would write a kernel module containing a function talk to the power switch over the network, and then register your function in the function pointer pm_power_off.


Triggering a system shutdown might be even more system dependent. It might be that the simplest way would be to invoke an existing command line program such as sutdown or poweroff.

Not the answer you're looking for? Browse other questions tagged .