A Python-based Windows service that monitors the battery status of your laptop. The service automatically puts the system to sleep when the laptop is running on battery power (i.e., not connected to a charger). The script runs in the background as a Windows service.
- Monitors the battery status in real-time.
- Automatically puts the system to sleep when the laptop is not charging.
- Runs as a Windows service that starts automatically when the system boots.
- Minimal resource usage and configurable for any number of checks.
- Windows operating system
- Python 3.x (tested with Python 3.10)
pywin32
package to create and manage the Windows servicewmi
package to access system information
git clone https://github.com/yourusername/battery-monitor-service.git
cd battery-monitor-service
pip install -r requirements.txt
python -m pywin32_postinstall
- Open bash terminal with administrator privileges
python battery_monitor_service.py install
- This registers the Python script as a Windows service.
- To start the service
python battery_monitor_service.py start
- Check the status
sc query BatteryMonitorService
- Alternatively, you can verify status/start the service in
serives.msc
- To stop the service
python battery_monitor_service.py stop
- If you encounter the error
MoveFileEx: Access is denied
during installation or service updates, ensure that you:
- Run the commands as an administrator.
- Stop the service before making updates.
- Manually copy the
pythonservice.exe
file from the lib\site-packages\win32 folder to your Python installation folder. - Worst case, restart your system and the problem should be resolved
To monitor the service logs, open the Windows Event Viewer:
- Press
Windows + R
and typeeventvwr.msc
to open the Event Viewer. - Navigate to Windows Logs > Application and look for logs with the source set to
Python Service
.
- If you make changes to the script, follow these steps to update the service:
python battery_monitor_service.py stop
-
Modify your source code as needed.
-
Start the service again
python battery_monitor_service.py start
- To uninstall the service:
python battery_monitor_service.py remove
- This will remove the service from Windows.
- Feel free to fork this repository, submit issues, and make pull requests. Any contributions are welcome!