Get the App
SLTechnology News&Howtos  ›  Servers  › 

Realization method of low Power automatic shutdown of Linux

Shulou Source: shulou.com Published: 2022-06-02 03:18:18 10月02日 Update

Preface

Recently, the electricity in my home is unstable, and the power is often cut off all of a sudden. Although I have a notebook, I may not be able to get an incoming call. It won't be long before the SSD in my notebook will be compared.

So write a simple script, when the power is low, turn off the phone, and then check regularly with crontab or systemd timers.

For more information on how to use the crontab command, please refer to here: https://www.jb51.net/article/148575.htm

I won't say much. Let's take a look at the detailed introduction.

Check_shutdown.timer:

$cat / etc/systemd/system/check_shutdown.timer [Unit] Description=Check if battery is low every 10 details [timer] OnCalendar=*:0/10Persistent= true [install] WantedBy=timers.target

Check_shutdown.service:

$cat / etc/systemd/system/check_shutdown.service [Service] ExecStart=ExecStart=/home/jiajun/.xmonad/scripts/shutdown.py

Check_shutdown.py:

#! / home/jiajun/.py3k/bin/pythonimport psutilimport loggingimport osimport datetimebat = psutil.sensors_battery () logging.warn ("% s: battery status:% s", datetime.datetime.now (), bat) if bat.percent < 15:logging.warn ("gonna shutdown") os.system ("sudo shutdown-h now")

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Tags: Content notes notebooks references learning methods electricity between value domicile usage preface commands that is questions articles scripts rhythm communication work Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn MySQL Apple Shulou Tech Info