Fast system shutdown in Python

2k views Asked by At

I'm designing a piece of electronic hardware that may come into contact with water if there is a mechanical malfunction (e.g. a seal breaks). Inside the device, I have moisture sensors around all critical areas. I've already written a script that monitors these moisture sensors, but I want to write an emergency script that if any of the moisture sensors sense water, the system can be shut off quickly to avoid electrical damage to the device itself and anyone/anything nearby.

At the moment, all I was able to find was the following protocol:

import os
os.system('shutdown -P now')

Is there a trivial way of making the computer have a 'hard' shut down?

I've also considered having the main power be fed to the device through a relay that the computer can control so I can just physically cut power to the computer, but I was hoping there was a way to do this without having to use such a heavy duty power relay.

2

There are 2 answers

0
mwweb On BEST ANSWER

Using System Request (SysRq) and than calling echo b > /proc/sysrq-trigger to immediately reboot the system, without unmounting or syncing filesystems.

import os

os.system('echo 1 > /proc/sys/kernel/sysrq && echo b > /proc/sysrq-trigger')

Check this answer for more info:

https://unix.stackexchange.com/questions/163370/is-there-a-fastest-way-to-shutdown-the-system

4
Abe On

Try this:

import os
os.system('shutdown -h now')