This project is a web-based battery monitor and automatic shutdown tool for Linux systems. It uses Tornado (Python web framework) to provide a simple web interface and background monitoring of battery status, with automatic shutdown scheduling when the battery is low.
- Web Interface:
- View current battery status via a web page (
index.html
). - REST endpoint at
/status
returns raw battery info.
- View current battery status via a web page (
- Automatic Shutdown:
- Monitors battery percentage and state every 5 minutes.
- If battery drops to 20% or below and is discharging, schedules a system shutdown in 2 hours.
- Cancels scheduled shutdown if battery condition improves.
- Logging:
- Logs all actions and battery status to
battery_shutdown.log
.
- Logs all actions and battery status to
- Linux system with
upower
andshutdown
utilities available. - Python 3.7+
- Tornado web framework
- Clone the repository:
git clone https://github.com/01one/battery-status-on-linux-laptop-as-a-server.git cd battery-status-on-linux-laptop-as-a-server
- Install dependencies:
pip install -r requirements.txt
-
Start the server:
python3 app.py
Note:
sudo
is required for shutdown commands. -
Access the web interface:
- Open your browser and go to: http://127.0.0.1:5000
-
API Endpoint:
- Battery status: http://127.0.0.1:5000/status
- The server checks battery status every 5 minutes using
upower
. - If battery is low and discharging, it schedules a shutdown in 2 hours using
shutdown -h +120
. - If battery condition improves, it cancels the scheduled shutdown using
shutdown -c
. - All actions are logged in
battery_shutdown.log
.
-
The app requires
sudo
privileges to execute shutdown commands. Consider configuringsudoers
to allow passwordless shutdown for the user running this script. -
For remote access, you can optionally set up a Cloudflare Tunnel and configure a custom domain. You may also use Nginx as a reverse proxy for added security and flexibility.
-
Setup as a service for automatic startup on system startup https://github.com/01one/toolbox/blob/main/setup_systemd_service.md
MIT License
Screenshot: