How to implement Linux automatic shutdown when the battery is low

How to implement Linux automatic shutdown when the battery is low

Preface

The electricity in my residence has been unstable recently, and there are frequent sudden power outages. Although I have a laptop, it may not be able to receive power. If this continues, the SSD in the laptop will soon fail.

So I wrote a simple script that shuts down the computer when the battery is low, and then uses crontab or systemd timers to check it regularly.

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

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 minutes

[Timer]
OnCalendar=*:0/10
Persistent=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/python

import psutil
import logging
import os
import datetime

bat = 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")

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Linux (Ubuntu) hibernation, suspend, standby, shutdown commands detailed introduction
  • PHP controls common functions of Linux servers, such as shutdown, restart, opening new sites, etc.
  • Easily master Linux shutdown and restart commands
  • Shell script in Linux to realize shutdown after downloading
  • Detailed explanation of Linux shutdown command summary
  • Linux Administrator's Guide (5) -- Booting and Shutting Down
  • Execute the specified script function when shutting down Linux
  • Detailed explanation of shutdown and restart commands in Linux
  • Linux uses the init command to shut down, restart, and switch modes

<<:  Installation of mysql-community-server. 5.7.18-1.el6 under centos 6.5

>>:  JavaScript implements mouse drag to adjust div size

Recommend

Using MySQL in Windows: Implementing Automatic Scheduled Backups

1. Write a backup script rem auther:www.yumi-info...

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

A detailed introduction to setting up Jenkins on Tencent Cloud Server

Table of contents 1. Connect to Tencent Cloud Ser...

Does the % in the newly created MySQL user include localhost?

Normal explanation % means any client can connect...

Use HTML to write a simple email template

Today, I want to write about a "low-tech&quo...

Solution to the problem that order by is not effective in MySQL subquery

By chance, I discovered that a SQL statement prod...

How are spaces represented in HTML (what do they mean)?

In web development, you often encounter characters...

Distributed monitoring system Zabbix uses SNMP and JMX channels to collect data

In the previous article, we learned about the pas...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

MySQL deadlock routine: inconsistent batch insertion order under unique index

Preface The essence of deadlock is resource compe...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

MySQL learning notes help document

View system help help contents mysql> help con...

A brief comparison of Props in React

Table of contents Props comparison of class compo...

Solution to the problem that the Vue page image does not display

When making a new version of the configuration in...